如何在pref中添加所选复选框

时间:2016-08-22 05:22:32

标签: android listview android-checkbox android-sharedpreferences

我正在尝试在pref中添加所选复选框,当用户关闭应用时,pref将清除,但是当我打开应用程序时,我将选中所有复选框,甚至pref无效,以下是我的代码

public class MainActivity extends AppCompatActivity {
    //http://androidopentutorials.com/android-how-to-store-list-of-values-in-sharedpreferences/

    private ListView lvData;
    private ArrayList<ModelUser> modelUsers;
    private ArrayList<ModelUser> modelUserArrayList;
    private ModelUser modelUser;
    private PrefsManager prefsManager;
    private ProgressDialog pDialog;
    private String url="";
    private MyCustomAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        pDialog = new ProgressDialog(this);
        // Showing progress dialog before making http request
        pDialog.setMessage("Loading...");
        pDialog.show();
        lvData = (ListView)findViewById(R.id.lvData);

        Loaddatas();


    }


    private void Loaddatas() {

        showpDialog();
        JsonArrayRequest req = new JsonArrayRequest( url,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.d("ress", response.toString());


                        // Parsing json
                        for (int i = 0; i < response.length(); i++) {
                            try {

                                JSONObject obj = response.getJSONObject(i);

                                String heder=obj.getString("filterName");

                                System.out.println("Hader"+heder);

                                JSONArray details=obj.getJSONArray("getParam");
                                modelUsers = new ArrayList<>();
                                modelUserArrayList =new ArrayList<ModelUser>();
                                for(int j=0;j<details.length();j++)
                                {

                                    JSONObject det=details.getJSONObject(j);
                                    modelUser = new ModelUser();

                                    modelUser.setUserName(det.getString("paramName"));
                                   // modelUser.setSelected(false);
                                   // modelUser.setId(i);

                                    modelUsers.add(modelUser);


                                }

                                mAdapter = new MyCustomAdapter(MainActivity.this,modelUsers);
                                lvData.setAdapter(mAdapter);
                                prefsManager = new PrefsManager();

                                modelUserArrayList = prefsManager.getFavorites(MainActivity.this);
                                if(modelUserArrayList != null) {

                                    for (int k = 0; k < modelUserArrayList.size(); k++) {

                                        Log.e("User Name ",""+modelUserArrayList.get(k).getUserName());
                                       Log.e("User SElected ",""+modelUserArrayList.get(k).isSelected());
                                      //  Log.e("User user Id ",""+modelUserArrayList.get(j).getId());
                                    }
                                }


                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }



                        hidepDialog();

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d("ErrorVolley", "Error: " + error.getMessage());
                Toast.makeText(getApplicationContext(),
                        error.getMessage(), Toast.LENGTH_SHORT).show();
                hidepDialog();

            }
        });

        MyApplication.getInstance().addToReqQueue(req, "jreq");
    }


    private void showpDialog() {
        if (!pDialog.isShowing())
            pDialog.show();
    }

    private void hidepDialog() {
        if (pDialog.isShowing())
            pDialog.dismiss();
    }

}

2 个答案:

答案 0 :(得分:1)

尝试这个,为pref部分:

SharedPreferences pref;
SharedPreferences.Editor editor;

//initialize
pref = PreferenceManager.getDefaultSharedPreferences(context);
editor = pref.edit();

//get value
String var = pref.getString("any_tag_name", "default_return_value");

//put value
editor.putString("any_tag_name", "value");
editor.commit();

答案 1 :(得分:0)

public static void setPreference(String name, String value) {
    SharedPreferences.Editor editor = getSharedPreferences(PREFS_NAME, 0).edit();
    editor.putString(name, value);
    editor.apply();
}

并使用apply代替提交,因为申请更快

获取它

public static String getPreference(String name, String defaultValue) {
    return getSharedPreferences(PREFS_NAME, 0).getString(name, defaultValue);
}