从listpreferences获取值显示在列表视图中

时间:2014-04-22 05:17:24

标签: java android listview

在我的项目中,我有一个按钮,当我点击按钮时我想在listview中添加'one'值,当我再次点击第二次时我想添加'two',这将继续最多添加5个值listview如图所示。在5个值后,我不想允许添加列表允许,我有另一个要求,即,当我长按项目时,我想删除列表视图中的特定项目。最后,这些所有值都保存在SharedPrerences中,当那时活动打开时,我想在列表视图中显示所有值。

为此,我有一个小问题,即,假设我首先插入3个值(一,二,三)并再次打开活动时单击后退按钮3值不显示但是当我点击按钮时它显示所有以前的最新点击的值(一,二,三)和四。我希望在调用活动时显示3个值。 example diagram

 mycode:
    public class ListViewDemo1 extends Activity {
        /** Called when the activity is first created. */
        Button btn;
        static int count;
        private ListView list;
        public static ArrayList<String>  values = new ArrayList<String>();;
        ArrayList countList = new ArrayList();
        private ArrayAdapter adapter;
        SharedPreferences shared;
        Editor editor;
        private static ArrayList<String> sharedList;
        @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();
            btn.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    // TODO Auto-generated method stub
                 sharedList = new ArrayList();  
                    count++;
                    if(count==1){
                        values.add("one");
                        countList.add(count);
                    }
                    if(count==2){
                        values.add("two");
                        countList.add(count);
                    }
                    if(count==3){
                        values.add("three");
                        countList.add(count);
                    }
                    if(count==4){
                        values.add("four");
                        countList.add(count);

                    }
                    if(count==5){
                        values.add("five");
                        countList.add(count);
                    }
                    if(count>5){
                        --count;
                        Toast.makeText(getApplicationContext(), ""+count, 100).show();
                    }
                    //put values to sharedpreferences
                    editor.putInt("SIZE", values.size());
                    for(int i=0;i<values.size();i++){
                        editor.putString("addr"+i, values.get(i));
                    }
                    editor.commit();
                    // getting values from sharedpreference
                    int size= shared.getInt("SIZE", 0);
                    for(int k=0;k<size;k++){
                        sharedList.add(shared.getString("addr"+k,"" ));
                    }
                    adapter=new   
     ArrayAdapter(ListViewDemo1.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;
                }
            });

        }

2 个答案:

答案 0 :(得分:2)

您需要在Oncreate()上填写listView。

试试此代码

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;
        }
    });

}
}

答案 1 :(得分:0)

您可以将数组保存到SharedPreferences,因此这里有一个解决方案:

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

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;  
        }  

}

将数组设置为SharedPreferences: 当您从列表中添加或删除某些内容时,请清除并将阵列更新为共享首选项。

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

从SharedPreferences获取数组: 总是通过从共享偏好中获取数组来加载数据,这样您就可以在应用程序中获得最新数据。

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

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