[英]How can i save each line in shared preferences instead of the last line?
我有这个国家名单
梵蒂冈城
中国
意大利
伊朗
韩国
西班牙
德国
法国
我将此列表保存在共享首选项中,然后调用它以将其放入列表视图中,
问题是,它只得到了最后一项,所以 France 将最后保存,它将是 listView 中唯一的一项
我该如何解决? 我知道循环,但我不擅长
任何有关代码的帮助将不胜感激!
试试这个代码:
主活动.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
= { "Red", "Green", "Black",
"Orange", "Blue", "Pink",
"Blush", "Brown", "Yellow" };
// Create the object of ArrayAdapter with String
// which hold the data as the list item.
ArrayAdapter<String> adapter
= new ArrayAdapter<String>(
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);
}
}
活动_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++}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.