简体   繁体   English

如何在列表视图的每个项目上分别保存切换按钮状态

[英]How to save toggle button state separately on each item of listview

I start a new activity clicking over an item in my listview. 我单击列表视图中的一个项目,开始一个新的活动。 When I open a new activity there is a toggle. 当我打开一个新活动时,会有一个切换。 I try to save the toggle state separately on each item of listview. 我尝试将切换状态分别保存在listview的每个项目上。 Should I use Shared Preference? 我应该使用共享首选项吗? But how? 但是如何?

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

btnAddToList = (ToggleButton) findViewById(R.id.btnAddToList);
    btnAddToList.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton toggleButton, boolean isChecked) {
            if(isChecked) {
                addEventToList();
                scheduleNotification(getNotification("The Event begin soon"), 7200000);
            }
            else {
                Toast.makeText(OneEvent.this, id_favorite_event, Toast.LENGTH_LONG).show();
            };
        }
    });
 }


 private void addEventToList(){
    final String user_id = id_user;
    final String event_id = id_event;

    class AddNewEventToList extends AsyncTask<Void,Void,String>{
        ProgressDialog loading;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            loading = ProgressDialog.show(OneEvent.this,"Please", "wait",false,false);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            loading.dismiss();
            String suc = getFavoriteEventId(s);
            if(suc.equalsIgnoreCase("success")){
                Toast.makeText(OneEvent.this, "Done", Toast.LENGTH_LONG).show();
            }else {
                Toast.makeText(OneEvent.this, s, Toast.LENGTH_LONG).show();
            }
        }

        @Override
        protected String doInBackground(Void... v) {
            HashMap<String,String> params = new HashMap<>();
            params.put(Config.LIST_COLUMN_USER_ID,user_id);
            params.put(Config.LIST_COLUMN_EVENT_ID, event_id);

            RequestHandler rh = new RequestHandler();
            String result = rh.sendPostRequest(Config.URL_ADD_TO_LIST, params);
            return result;
        }

        private String getFavoriteEventId(String json) {
            try {
                JSONObject jsonObject = new JSONObject(json);
                JSONArray result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY_FOR_FAVORITE_EVENT_ID);
                JSONObject c = result.getJSONObject(0);
                id_favorite_event = c.getString(Config.TAG_GET_FAVORITE_EVENT_ID);
                json = "success";
                return json;

            } catch (JSONException e) {
                e.printStackTrace();
            }
            return json;
        }
    }
    AddNewEventToList anetl = new AddNewEventToList();
    anetl.execute();
}

You can set some Object, a boolean to indicate checked in this case to the view in the list view. 您可以将某些Object(布尔值)设置为列表视图中的视图,以指示在这种情况下为选中状态。 You can use setTag to set a tag to the view. 您可以使用setTag将标签设置为视图。 You can get back the value from it using getTag . 您可以使用getTag从中获取值。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM