如何将每一行保存在共享首选项中,而不是仅保存最后一行?

huangapple 未分类评论55阅读模式
英文:

How can i save each line in shared preferences instead of the last line?

问题

我有这个国家列表

梵蒂冈城

中国

意大利

伊朗

韩国

西班牙

德国

法国

我将这个列表保存在共享偏好中,然后调用它放入列表视图,

问题是,它只得到了最后一项,所以法国会最后被保存,它将是列表视图中唯一的项

我该如何解决这个问题?我知道可以使用循环,但我不擅长它

对于代码的任何帮助将不胜感激!

英文:

I have this list of countries

> Vatican City
>
> China
>
> Italy
>
> Iran
>
> S. Korea
>
> Spain
>
> Germany
>
> France

i save this list in shared preferences then call it to put it in listview,

the problem is, it got only the last item, so France will saved last and it will be the only one in the listView

How can i solve that? i know with loop, but i'm not good at it

Any help with codes will be appreciated !

答案1

得分: 0

试试这段代码:

MainActivity.java

import android.arch.lifecycle.AndroidViewModel; 
import android.graphics.Color; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.widget.ArrayAdapter; 
import android.widget.AutoCompleteTextView; 

public class MainActivity extends AppCompatActivity { 

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 

        // 通过ID获取在xml文件中分配的AutoCompleteTextView
        AutoCompleteTextView 
            autoCompleteTextView 
            = (AutoCompleteTextView) 
                findViewById( 
                    R.id.autocompleteTextView); 

        // 创建字符串数组并存储值
        String[] colors 
            = { "Red", "Green", "Black", 
                "Orange", "Blue", "Pink", 
                "Blush", "Brown", "Yellow" }; 

        // 使用字符串的ArrayAdapter对象
        // 作为列表项的数据保持数据
        ArrayAdapter<String> adapter 
            = new ArrayAdapter<String>( 
                this, 
                android.R.layout.select_dialog_item, 
                colors); 

        // 在1个字之后提供建议
        autoCompleteTextView.setThreshold(1); 

        // 将适配器设置为数据的列表
        autoCompleteTextView.setAdapter(adapter); 
        autoCompleteTextView.setTextColor(Color.BLACK); 
    } 
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"> 

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="12dp"
        android:layout_marginLeft="80dp"
        android:text="Write the color name !"
        android:textSize="20dp"
        android:textStyle="bold" /> 

    <AutoCompleteTextView

        android:id="@+id/autocompleteTextView"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="120dp"
        android:layout_marginLeft="90dp"
        /> 

</RelativeLayout>

有关更多详情,请查看这里:https://www.geeksforgeeks.org/android-auto-complete-textbox-and-how-to-create-it/

如果你想获取保存在共享首选项中的数据,请使用以下代码:

val j=adapter.count
var i=0
while (i<j){
    Log.d("abc",adapter.getItem(i))
    i++
}
英文:

Try this code:

MainActivity.java

import android.arch.lifecycle.AndroidViewModel; 
import android.graphics.Color; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.widget.ArrayAdapter; 
import android.widget.AutoCompleteTextView; 

public class MainActivity extends AppCompatActivity { 

	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{ 
		super.onCreate(savedInstanceState); 
		setContentView(R.layout.activity_main); 

		// By ID get the AutoCompleteTextView 
		// which id is assign in xml file 
		AutoCompleteTextView 
			autoCompleteTextView 
			= (AutoCompleteTextView) 
				findViewById( 
					R.id.autocompleteTextView); 

		// Create the string array 
		// and store the values. 
		String[] colors 
			= { &quot;Red&quot;, &quot;Green&quot;, &quot;Black&quot;, 
				&quot;Orange&quot;, &quot;Blue&quot;, &quot;Pink&quot;, 
				&quot;Blush&quot;, &quot;Brown&quot;, &quot;Yellow&quot; }; 

		// Create the object of ArrayAdapter with String 
		// which hold the data as the list item. 
		ArrayAdapter&lt;String&gt; adapter 
			= new ArrayAdapter&lt;String&gt;( 
				this, 
				android.R.layout.select_dialog_item, 
				colors); 

		// Give the suggestion after 1 words. 
		autoCompleteTextView.setThreshold(1); 

		// Set the adapter for data as a list 
		autoCompleteTextView.setAdapter(adapter); 
		autoCompleteTextView.setTextColor(Color.BLACK); 
	} 
}

activity_main.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt; 
&lt;RelativeLayout
	xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
	xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
	xmlns:tools=&quot;http://schemas.android.com/tools&quot;
	android:layout_width=&quot;match_parent&quot;
	android:layout_height=&quot;match_parent&quot;
	tools:context=&quot;.MainActivity&quot;&gt; 

	&lt;TextView
		android:layout_width=&quot;wrap_content&quot;
		android:layout_height=&quot;wrap_content&quot;
		android:layout_marginTop=&quot;12dp&quot;
		android:layout_marginLeft=&quot;80dp&quot;
		android:text=&quot;Write the color name !&quot;
		android:textSize=&quot;20dp&quot;
		android:textStyle=&quot;bold&quot; /&gt; 

	&lt;AutoCompleteTextView

		android:id=&quot;@+id/autocompleteTextView&quot;
		android:layout_width=&quot;200dp&quot;
		android:layout_height=&quot;wrap_content&quot;
		android:layout_marginTop=&quot;120dp&quot;
		android:layout_marginLeft=&quot;90dp&quot;
		/&gt; 

&lt;/RelativeLayout&gt; 

See here for more details: https://www.geeksforgeeks.org/android-auto-complete-textbox-and-how-to-create-it/

If you want to get data which are saved in shared preferences use this code:

val j=adapter.count
            var i=0
            while (i&lt;j){
            Log.d(&quot;abc&quot;,adapter.getItem(i))
            i++}

huangapple
  • 本文由 发表于 2020年3月16日 08:33:19
  • 转载请务必保留本文链接:https://java.coder-hub.com/60699053.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定