Java Android中使用SharedReferences的动态列表

Java Android中使用SharedReferences的动态列表,java,android,listview,dynamic,sharedpreferences,Java,Android,Listview,Dynamic,Sharedpreferences,我正在开发一个需要创建动态列表的项目。为此,我使用共享首选项将值永久存储在列表中。但当我重新启动项目时,输入的值不会退出。我尝试了很多次,但问题没有解决。 这是我的代码。 numberlistativity.java public class NumberListActivity extends Activity { ListView numList; ArrayList<String> list = new ArrayList<String>();

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

    }

}      
public class numberStactivity扩展活动{
列表视图numList;
ArrayList=新建ArrayList();
阵列适配器;
公共静态最终字符串Place=“placeKey”;
按钮btnAdd;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_列表);
numList=(ListView)findViewById(R.id.list);
btnAdd=(按钮)findviewbyd(R.id.btnAdd);
适配器=新阵列适配器(此,
android.R.layout.simple_list_item_1,list);
numList.setAdapter(适配器);
setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
SharedReferences首选项=GetSharedReferences(“位置”,
上下文。模式(私人);
SharedReferences.Editor spEditor=preferences.edit();
EditText edit=(EditText)findViewById(R.id.txtItem);
putString(“Value”,edit.getText().toString());
提交();
添加(preferences.getString(“Value”和“));
adapter.notifyDataSetChanged();
}
});
list.toArray();
}
}      
custom_list.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();

        }
    });
}
SharedReferences首选项;
SharedReferences.Editor spEditor;
整数计数=0;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_列表);
首选项=GetSharedReferences(“地点”,Context.MODE\u PRIVATE);
spEditor=preferences.edit();
count=preferences.getInt(“count”,0);
如果(计数>0){
for(int i=0;i
@NirHartmann:这是在列表中存储值的完整代码。@NirHartmann:这两行将数据添加到列表中。**list.add(preferences.getString(“value”),“”);adapter.notifyDataSetChanged()**@ZiaUrRehman看看我的ans