我有自定义警报dailog列表并检查我是否尝试在pref中存储所选复选框,如何在共享首选项中存储所选项目,我可以从列表中选择值?任何人都可以帮助我吗?
class LoadAlldata extends AsyncTask<String, String, ArrayList<HashMap<String, String>>> {
ArrayAdapter<String> adapterallstates;
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(TestCountry.this);
pDialog.setMessage("Please..");
pDialog.setIndeterminate(true);
// pDialog.setIndeterminateDrawable(getResources().getDrawable(R.drawable.custom_progress));
pDialog.setCancelable(true);
pDialog.show();
}
protected ArrayList<HashMap<String, String>> doInBackground(String... args) {
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
statedata = new ArrayList<HashMap<String, String>>();
String jsonStr = sh.makeServiceCall(STATE_URL, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
jsonObj = new JSONArray(jsonStr);
// state_list = jsonObj.getJSONArray(COUNTRY_LIST);
// looping through All Contacts
for (int i = 0; i < jsonObj.length(); i++) {
JSONObject c = jsonObj.getJSONObject(i);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
map.put(STATE_ID, c.getString(STATE_ID));
map.put(STATE_NAME, c.getString(STATE_NAME));
statedata.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return statedata;
}
protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
super.onPostExecute(result);
pDialog.dismiss();
final String[] arrallstates = new String[statedata.size()];
checked_state=new boolean[statedata.size()];
for (int index = 0; index < statedata.size(); index++) {
HashMap<String, String> map = statedata.get(index);
arrallstates[index] = map.get(STATE_NAME);
}
txtvw.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View w) {
new AlertDialog.Builder(TestCountry.this)
.setTitle("Choose a Days")
.setMultiChoiceItems(arrallstates, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
// TODO Auto-generated method stub
//storing the checked state of the items in an array
checked_state[which] = isChecked;
}
})
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
String display_checked_days = "";
for (int i = 0; i < arrallstates.length; i++) {
if (checked_state[i] == true) {
display_checked_days = display_checked_days + " " + arrallstates[i];
}
}
Toast.makeText(TestCountry.this, "The selected is" + display_checked_days, Toast.LENGTH_SHORT).show();
//clears the array used to store checked state
for (int i = 0; i < checked_state.length; i++) {
if (checked_state[i] == true) {
checked_state[i] = false;
}
}
//used to dismiss the dialog upon user selection.
dialog.dismiss();
}
}).create().show();;
}
});
}
}
答案 0 :(得分:0)
首先在Adapter中手动编写setSelection方法。
private int selectedItem = -1;
public void setSelectedItem(int position) {
selectedItem = position;
notifyDataSetChanged();
} // Here selectedItem is global variable
在onBindViewHolder中添加以下代码:
if (position == selectedItem){
viewHolder.chkSelected.setChecked(stList.get(position));
viewHolder.chkSelected.setTag(stList.get(position));
} else {
viewHolder.chkSelected.setChecked(stList.get(position).isselected());
viewHolder.chkSelected.setTag(stList.get(position));
}
从您的Activity中,您必须从适配器insance调用setSelectedItem(position)。为了获得位置,您已经知道了chekbox Id,因此您可以从列表中获取该Id的位置。
从列表中获取位置:
private int getSelectedItem(List<String> stList, String name) {
int pos = -1;
for(int i=0; i<stList.size(); i++) {
if(name.equals(stList.get(i)){
pos = i;
break;
}
}
return pos;
}
答案 1 :(得分:0)
试试这样:
首先使用getter setter创建模型类:
private boolean checked;
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
在getview的Adapter Class中写这样的代码。
FilterModelClass model=stList.get(position);
viewHolder.chkbox.setOnCheckedChangeListener(new CheckUpdateListener(model));
使类如下所示处理checkedChangedListener
/*******************
* Checkbox Checked Change Listener
********************/
private final class CheckUpdateListener implements CompoundButton.OnCheckedChangeListener {
private final FilterModelClass model;
private CheckUpdateListener(FilterModelClass model) {
this.model = model;
}
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.i("onCheckedChanged", "isChecked: " + isChecked);
model.setChecked(isChecked);
notifyDataSetChanged();
}
}