我点击了listview中的项目,开始新的活动。当我打开一个新活动时,会有一个切换。我尝试在listview的每个项目上单独保存切换状态。我应该使用共享偏好吗?但是如何?
@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();
}
答案 0 :(得分:0)
您可以设置一些Object,一个布尔值,表示在这种情况下检查列表视图中的视图。您可以使用setTag
为视图设置标记。您可以使用getTag
从中获取值。希望这会有所帮助。