Java 从列表视图中显示的SharedReferences获取值

Java 从列表视图中显示的SharedReferences获取值,java,android,listview,Java,Android,Listview,在我的项目中,我有一个按钮,当我单击按钮时,我想向listview添加“一”值,当我再次单击第二次时,我想添加“两”,这将继续向listview添加多达5个值,如图所示。在5个值之后,我不想允许添加列表允许,我还有另一个要求,即,当我长按item时,我想删除listview中的特定项。最后,这些值保存在SharedReferences中,当活动打开时,我想在列表视图中显示所有值 对于这一点,我有一个小问题,即,假设首先我插入3个值1、2、3,并在再次打开“活动”时单击“后退”按钮,3个值不会显示

在我的项目中,我有一个按钮,当我单击按钮时,我想向listview添加“一”值,当我再次单击第二次时,我想添加“两”,这将继续向listview添加多达5个值,如图所示。在5个值之后,我不想允许添加列表允许,我还有另一个要求,即,当我长按item时,我想删除listview中的特定项。最后,这些值保存在SharedReferences中,当活动打开时,我想在列表视图中显示所有值

对于这一点,我有一个小问题,即,假设首先我插入3个值1、2、3,并在再次打开“活动”时单击“后退”按钮,3个值不会显示,但当我单击按钮时,它会显示所有以前的值1、2、3和4,以供最近一次单击。我想在调用任何活动时显示3个值。


您可以将阵列保存到SharedReferences,因此这里有一个解决方案:

用于设置和获取数组的SharedReferences类:

public class SharedPrefManager {

        public static boolean saveArray(String[] array, String arrayName, Context mContext) {   
            SharedPreferences prefs = mContext.getSharedPreferences("preferencename", 0);  
            SharedPreferences.Editor editor = prefs.edit();  
            editor.putInt(arrayName +"_size", array.length);  
            for(int i=0;i<array.length;i++)  
                editor.putString(arrayName + "_" + i, array[i]);  
            return editor.commit();  
        } 

        public static String[] loadArray(String arrayName, Context mContext) {  
            SharedPreferences prefs = mContext.getSharedPreferences("preferencename", 0);  
            int size = prefs.getInt(arrayName + "_size", 0);  
            String array[] = new String[size];  
            for(int i=0;i<size;i++)  
                array[i] = prefs.getString(arrayName + "_" + i, null);  
            return array;  
        }  

}
从SharedReferences获取数组: 始终通过从SharedReferences获取数组来加载数据,以便在应用程序中获得最新数据

String [] a = SharedPrefManager.loadArray("list", getApplicationContext());

要删除某些内容,您只需从SharedReference获取数组,清除SharedReference数据,从数组中删除所需的值,然后将其保存到SharedReference。

您需要在Oncreate上填写listView

//convert your list to array
String [] array = list.toArray(new String[list.size()]);
//save array to shared pref
SharedPrefManager.saveArray(array, "list", getApplicationContext());
试试这个代码

public class MainActivity extends Activity {
/** Called when the activity is first created. */
Button btn;
static int count = 0;
private ListView list;
public static ArrayList<String> values = new ArrayList<String>();;
ArrayList<Integer> countList = new ArrayList<Integer>();
private ArrayAdapter<String> adapter;
SharedPreferences shared;
Editor editor;
private static ArrayList<String> sharedList = new ArrayList<String>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    list = (ListView) findViewById(R.id.list);
    btn = (Button) findViewById(R.id.btn);
    shared = this.getSharedPreferences("Myprefernces",
            Context.MODE_WORLD_WRITEABLE);
    editor = shared.edit();

    sharedList.clear();
    int size = shared.getInt("SIZE", 0);
    count = size;
    for (int k = 0; k < size; k++) {
        sharedList.add(shared.getString("addr" + k, ""));
    }
    adapter = new ArrayAdapter<String>(MainActivity.this,
            android.R.layout.simple_list_item_1, sharedList);
    list.setAdapter(adapter);

    btn.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            count++;

            if (count > 5) {
                --count;
                Toast.makeText(getApplicationContext(), "" + count, 100)
                        .show();
            } else {
                String string = "one";
                if (count == 1) {
                    values.add("one");
                    countList.add(count);
                    string = "one";
                }
                if (count == 2) {
                    values.add("two");
                    countList.add(count);
                    string = "two";
                }
                if (count == 3) {
                    values.add("three");
                    countList.add(count);
                    string = "three";
                }
                if (count == 4) {
                    values.add("four");
                    countList.add(count);
                    string = "four";

                }
                if (count == 5) {
                    values.add("five");
                    countList.add(count);
                    string = "five";
                }

                // put values to sharedpreferences
                // editor.putInt("SIZE", values.size());
                editor.putInt("SIZE", count);

                editor.putString("addr" + (count - 1), string);
                // for (int i = 0; i < values.size(); i++) {
                // editor.putString("addr" + count, values.get(i));
                // }
                editor.commit();
                // getting values from sharedpreference

                sharedList.clear();

                int size = shared.getInt("SIZE", 0);
                for (int k = 0; k < size; k++) {
                    sharedList.add(shared.getString("addr" + k, ""));
                }
                adapter = new ArrayAdapter<String>(MainActivity.this,
                        android.R.layout.simple_list_item_1, sharedList);
                list.setAdapter(adapter);
            }

        }
    });
    list.setOnItemLongClickListener(new OnItemLongClickListener() {

        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            // TODO Auto-generated method stub
            --count;
            // values.remove(arg2);
            sharedList.remove(arg2);
            editor.clear();
            editor.commit();
            editor.putInt("SIZE", sharedList.size());
            for (int i = 0; i < sharedList.size(); i++) {
                editor.putString("addr" + i, sharedList.get(i));
            }
            editor.commit();
            adapter.notifyDataSetChanged();

            return true;
        }
    });

}
}

请检查一下我的代码,我只使用了SharedReferences,我想你没有读我的问题completely@kumar我把你的问题看完了。无论何时在列表中添加/删除内容,都要更新SharedReferences值。每当您想要通知/创建listview时,从SharedReferences加载数据。因此,当您按back并再次打开“活动”时,您将从您的SharedReferences中获得最新数据。我可以了解为什么我获得了2张反对票吗?我展示了一种设置和获取SharedReferences值的方法。任何缺乏或误导性的信息?我想不是。请分开。当我在oncreate上填写listview时,感谢您的响应。它给出了nullpointer异常,因为我在button onclick listener中创建了sharedList对象。如何克服此问题请告诉我您可以在oncreate中创建sharedList对象。我想我犯了错误。无需覆盖backPressed事件只需在onCreate中填充您的listview如果我在onCreate中创建sharedList对象,当我第一次单击按钮获取一个值时,第二次单击获取两个值上一个值和最新值,第三次单击获取三个值两个以前的值和一个最新值Remove sharedList=new ArrayList;点击按钮。你的问题不清楚-试着突出问题的重要部分。从我所看到的,当显示活动时,您在显示列表时遇到问题?是的,这就是问题所在尝试从onResume更新列表-每当显示列表时都会调用此函数
public class MainActivity extends Activity {
/** Called when the activity is first created. */
Button btn;
static int count = 0;
private ListView list;
public static ArrayList<String> values = new ArrayList<String>();;
ArrayList<Integer> countList = new ArrayList<Integer>();
private ArrayAdapter<String> adapter;
SharedPreferences shared;
Editor editor;
private static ArrayList<String> sharedList = new ArrayList<String>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    list = (ListView) findViewById(R.id.list);
    btn = (Button) findViewById(R.id.btn);
    shared = this.getSharedPreferences("Myprefernces",
            Context.MODE_WORLD_WRITEABLE);
    editor = shared.edit();

    sharedList.clear();
    int size = shared.getInt("SIZE", 0);
    count = size;
    for (int k = 0; k < size; k++) {
        sharedList.add(shared.getString("addr" + k, ""));
    }
    adapter = new ArrayAdapter<String>(MainActivity.this,
            android.R.layout.simple_list_item_1, sharedList);
    list.setAdapter(adapter);

    btn.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            count++;

            if (count > 5) {
                --count;
                Toast.makeText(getApplicationContext(), "" + count, 100)
                        .show();
            } else {
                String string = "one";
                if (count == 1) {
                    values.add("one");
                    countList.add(count);
                    string = "one";
                }
                if (count == 2) {
                    values.add("two");
                    countList.add(count);
                    string = "two";
                }
                if (count == 3) {
                    values.add("three");
                    countList.add(count);
                    string = "three";
                }
                if (count == 4) {
                    values.add("four");
                    countList.add(count);
                    string = "four";

                }
                if (count == 5) {
                    values.add("five");
                    countList.add(count);
                    string = "five";
                }

                // put values to sharedpreferences
                // editor.putInt("SIZE", values.size());
                editor.putInt("SIZE", count);

                editor.putString("addr" + (count - 1), string);
                // for (int i = 0; i < values.size(); i++) {
                // editor.putString("addr" + count, values.get(i));
                // }
                editor.commit();
                // getting values from sharedpreference

                sharedList.clear();

                int size = shared.getInt("SIZE", 0);
                for (int k = 0; k < size; k++) {
                    sharedList.add(shared.getString("addr" + k, ""));
                }
                adapter = new ArrayAdapter<String>(MainActivity.this,
                        android.R.layout.simple_list_item_1, sharedList);
                list.setAdapter(adapter);
            }

        }
    });
    list.setOnItemLongClickListener(new OnItemLongClickListener() {

        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            // TODO Auto-generated method stub
            --count;
            // values.remove(arg2);
            sharedList.remove(arg2);
            editor.clear();
            editor.commit();
            editor.putInt("SIZE", sharedList.size());
            for (int i = 0; i < sharedList.size(); i++) {
                editor.putString("addr" + i, sharedList.get(i));
            }
            editor.commit();
            adapter.notifyDataSetChanged();

            return true;
        }
    });

}
}