ListView
写真、テキストフィールド、および を含むアプリケーションがありますCheckBox
。の状態を保存したいCheckBox
。にsetOnCheckedChangeListener()
割り当てて行いCheckBox
ます。チェックボックスをクリックするとすべて正常に動作し、ログに保存した後にプロパティを読みました。
この問題は、アプリを開いて 1 つ以上のチェックボックスを選択し、[戻る] ボタンをクリックして終了すると発生します。アプリケーションを開くと、チェックボックスが間違っています。閉じてもう一度開くと、エラーが発生します。保存するために、変数の一意の ID を持っています。
手伝ってくれますか?
ItemListBaseAdapter
:public class ItemListBaseAdapter extends BaseAdapter { private static ArrayList<ItemDetails> itemDetailsrrayList; private Integer[] imgid = { R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5, R.drawable.img6 }; private LayoutInflater l_Inflater; private Context _context; private SharedPreferences _apref; private Editor _editor; private String _NOMBREPREF = "perroo"; private String _CADINICIO = "gatoo"; public ItemListBaseAdapter(Context context, ArrayList<ItemDetails> results) { _context = context; itemDetailsrrayList = results; l_Inflater = LayoutInflater.from(context); _apref = _context.getSharedPreferences(_NOMBREPREF, Context.MODE_PRIVATE); _editor = _apref.edit(); } public int getCount() { return itemDetailsrrayList.size(); } public Object getItem(int position) { return itemDetailsrrayList.get(position); } public long getItemId(int position) { return position; } public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = l_Inflater.inflate(R.layout.item_details_view, null); holder = new ViewHolder(); holder.txt_itemName = (TextView) convertView.findViewById(R.id.name); holder.txt_itemDescription = (TextView) convertView.findViewById(R.id.itemDescription); holder.txt_itemPrice = (TextView) convertView.findViewById(R.id.price); holder.itemImage = (ImageView) convertView.findViewById(R.id.photo); holder.ckb_degustado = (CheckBox) convertView.findViewById(R.id.checkBox1); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.txt_itemName.setText(itemDetailsrrayList.get(position).getName()); holder.txt_itemDescription.setText(itemDetailsrrayList.get(position).getItemDescription()); holder.txt_itemPrice.setText(itemDetailsrrayList.get(position).getPrice()); holder.itemImage.setImageResource(imgid[itemDetailsrrayList.get(position).getImageNumber() - 1]); boolean tmprf = readPrefsCheckbox(itemDetailsrrayList.get(position).getIduniko().toString()); String tmpr2 = Boolean.toString(tmprf); holder.ckb_degustado.setChecked(tmprf); Log.d("DREKION", "Inicio: " + itemDetailsrrayList.get(position).getIduniko().toString() + " " + tmpr2); // Checkbox degustation clickado holder.ckb_degustado.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub String cad1; boolean cadtmp; String cadtmpp; if (isChecked == true) { cad1 = _CADINICIO + itemDetailsrrayList.get(position).getIduniko().toString(); Log.d("DREKION", "True: " + cad1); _editor.putBoolean(cad1, true); _editor.commit(); cadtmp = readPrefsCheckbox(itemDetailsrrayList.get(position).getIduniko().toString()); cadtmpp = Boolean.toString(cadtmp); Log.d("DREKION", "True despues: " + cadtmpp); } else { cad1 = _CADINICIO + itemDetailsrrayList.get(position).getIduniko().toString(); Log.d("DREKION", "False: " + cad1); _editor.putBoolean(cad1, false); _editor.commit(); cadtmp = readPrefsCheckbox(itemDetailsrrayList.get(position).getIduniko().toString()); cadtmpp = Boolean.toString(cadtmp); Log.d("DREKION", "False despues: " + cadtmpp); } } }); convertView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // do something here // for communication with Fragment/Activity, you can use your // own listener /* * Toast.makeText(v.getContext(), "Has elegido : " + * itemDetailsrrayList.get(position).getItemDescription() + "", * Toast.LENGTH_LONG).show(); Intent intent = new * Intent(v.getContext(), ItemMoreInfo.class); * intent.putExtra("objetito", * itemDetailsrrayList.get(position)); */ sendMessage(v, itemDetailsrrayList.get(position)); } }); return convertView; } private boolean readPrefsCheckbox(String _iduniko) { return _apref.getBoolean(_CADINICIO + _iduniko, false); } public void sendMessage(View view, ItemDetails objID) { Intent intent = new Intent(view.getContext(), ItemMoreInfo.class); intent.putExtra("objetito", objID); view.getContext().startActivity(intent); } private void showNotificacion(Context c, String t) { Toast.makeText(c, t, Toast.LENGTH_LONG).show(); } static class ViewHolder { TextView txt_itemName; TextView txt_itemDescription; TextView txt_itemPrice; ImageView itemImage; CheckBox ckb_degustado; } }
MainActivity
:public class MainActivity extends Activity { private SharedPreferences pref; private Editor editor; private String _NOMBREPREF = "perroo"; private String _CADINICIO = "gatoo"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ArrayList<ItemDetails> image_details = GetSearchResults(); // Init shares vars pref = getApplicationContext().getSharedPreferences(_NOMBREPREF, 0); editor = pref.edit(); final ListView lv1 = (ListView) findViewById(R.id.listV_main); lv1.setAdapter(new ItemListBaseAdapter(this, image_details)); // lv1.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> a, View v, int position, long id) { /* * * Object o = lv1.getItemAtPosition(position); ItemDetails * obj_itemDetails = (ItemDetails)o; * Toast.makeText(MainActivity.this, "Has elegido : " + " " + * obj_itemDetails.getName(), Toast.LENGTH_LONG).show(); * sendMessage * (findViewById(android.R.id.content),obj_itemDetails); */ } }); } private ArrayList<ItemDetails> GetSearchResults() { ArrayList<ItemDetails> results = new ArrayList<ItemDetails>(); ItemDetails item_details = new ItemDetails(); item_details.setIduniko("tap001"); item_details.setName("Pizza"); item_details.setItemDescription("prueba"); item_details.setPrice("2€"); item_details.setImageNumber(1); results.add(item_details); } }