英文:
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
= { "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);
}
}
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>
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<j){
Log.d("abc",adapter.getItem(i))
i++}
专注分享java语言的经验与见解,让所有开发者获益!
评论