gpt4 book ai didi

java - Android 中使用 SharedPreferences 的动态列表

转载 作者:行者123 更新时间:2023-12-01 13:12:01 25 4
gpt4 key购买 nike

我正在开发一个需要创建动态列表的项目。为此,我使用共享首选项将值永久存储在列表中。但是当我重新启动项目时,我会使用该值输入不退出。我尝试了很多次,但问题没有解决。
这是我的代码。
NumberListActivity.java

public class NumberListActivity extends Activity {
ListView numList;
ArrayList<String> list = new ArrayList<String>();
ArrayAdapter<String> adapter;
public static final String Place = "placeKey";
Button btnAdd;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_list);
numList = (ListView) findViewById(R.id.list);
btnAdd = (Button) findViewById(R.id.btnAdd);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, list);
numList.setAdapter(adapter);
btnAdd.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SharedPreferences preferences = getSharedPreferences("place",
Context.MODE_PRIVATE);
SharedPreferences.Editor spEditor = preferences.edit();
EditText edit = (EditText) findViewById(R.id.txtItem);
spEditor.putString("Value", edit.getText().toString());
spEditor.commit();
list.add(preferences.getString("Value", ""));
adapter.notifyDataSetChanged();

}
});
list.toArray();

}

}

自定义列表.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<EditText
android:id="@+id/txtItem"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:inputType="text" />

<Button
android:id="@+id/btnAdd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/txtItem" />

<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/txtItem" />

<TextView
android:id="@+id/empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/txtItem"
android:gravity="center_horizontal" />

</RelativeLayout>

最佳答案

试试这个..

SharedPreferences preferences;
SharedPreferences.Editor spEditor;
int count = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_list);

preferences = getSharedPreferences("place", Context.MODE_PRIVATE);
spEditor = preferences.edit();
count = preferences.getInt("count", 0);
if(count > 0){
for(int i = 0; i < count; i++){
list.add(preferences.getString("Value["+i+"]", ""));
}
}

numList = (ListView) findViewById(R.id.list);
btnAdd = (Button) findViewById(R.id.btnAdd);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, list);
numList.setAdapter(adapter);
btnAdd.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText edit = (EditText) findViewById(R.id.txtItem);
spEditor.putString("Value["+count+"]", edit.getText().toString());
spEditor.commit();
list.add(preferences.getString("Value["+count+"]", ""));
count += 1;
spEditor.putInt("count", count);
adapter.notifyDataSetChanged();

}
});
}

关于java - Android 中使用 SharedPreferences 的动态列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22776830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com