2017-05-27 10 views
1

私はListview内でチェックボックスを作成しました。しかし、私は、アプリケーションを終了し、再オープン、それは私がリストした項目の状態を保存しません。ListViewチェックボックスの状態を保存します。

これは私にとってはうまくいきませんでした。この問題を解決する方法については答えが見つからないようです。以下のように誰かがListAdapter Activtyのコードで私を助けることができたら、それは感謝します。

public class ListAdapter extends BaseAdapter { 
Context ctx; 
LayoutInflater lInflater; 
ArrayList<Product> objects; 

ListAdapter(Context context, ArrayList<Product> products) { 
    ctx = context; 
    objects = products; 
    lInflater = (LayoutInflater) ctx 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
} 

@Override 
public int getCount() { 
    return objects.size(); 
} 

@Override 
public Object getItem(int position) { 
    return objects.get(position); 
} 

@Override 
public long getItemId(int position) { 
    return position; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View view = convertView; 
    if (view == null) { 
     view = lInflater.inflate(R.layout.item, parent, false); 
    } 

    Product p = getProduct(position); 

    ((TextView) view.findViewById(R.id.tvDescr)).setText(p.name); 
    ((TextView) view.findViewById(R.id.tvPrice)).setText(p.price + ""); 
    ((ImageView) view.findViewById(R.id.ivImage)).setImageResource(p.image); 

    CheckBox cbBuy = (CheckBox) view.findViewById(R.id.cbBox); 
    cbBuy.setOnCheckedChangeListener(myCheckChangList); 
    cbBuy.setTag(position); 
    cbBuy.setChecked(p.box); 
    return view; 
} 

Product getProduct(int position) { 
    return ((Product) getItem(position)); 
} 

ArrayList<Product> getBox() { 
    ArrayList<Product> box = new ArrayList<Product>(); 
    for (Product p : objects) { 
     if (p.box) 
      box.add(p); 
    } 
    return box; 
} 

OnCheckedChangeListener myCheckChangList = new OnCheckedChangeListener() { 
    public void onCheckedChanged(CompoundButton buttonView, 
           boolean isChecked) { 
     getProduct((Integer) buttonView.getTag()).box = isChecked; 
    } 
}; 
} 

は私が何か間違ったことをしたのかはわからないか、私は間違った場所にまたは全体のコードはこれを行うに誤っている場合にそれを置く場合。

これは、共有設定を使用して編集ソリューションバージョンです:

public class ListAdapter extends BaseAdapter { 
Context ctx; 
LayoutInflater lInflater; 
ArrayList<Product> objects; 

ListAdapter(Context context, ArrayList<Product> products) { 
    ctx = context; 
    objects = products; 
    lInflater = (LayoutInflater) ctx 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
} 

@Override 
public int getCount() { 
    return objects.size(); 
} 

@Override 
public Object getItem(int position) { 
    return objects.get(position); 
} 

@Override 
public long getItemId(int position) { 
    return position; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View view = convertView; 
    if (view == null) { 
     view = lInflater.inflate(R.layout.item, parent, false); 
    } 

    final Product p = getProduct(position); 

    ((TextView) view.findViewById(R.id.tvDescr)).setText(p.name); 
    ((TextView) view.findViewById(R.id.tvPrice)).setText(p.price + ""); 
    ((ImageView) view.findViewById(R.id.ivImage)).setImageResource(p.image); 

    final CheckBox cbBuy = (CheckBox) view.findViewById(R.id.cbBox); 
    SharedPreferences settings = ctx.getSharedPreferences("data",ctx.MODE_PRIVATE); 
    boolean Checked = settings.getBoolean(p.name, false); 
    cbBuy.setChecked(Checked); 



    cbBuy.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      if(cbBuy.isChecked()==true){ 
       SharedPreferences settings = ctx.getSharedPreferences("data",Context.MODE_PRIVATE); 
       settings.edit().putBoolean(p.name, true).commit(); 
       Toast.makeText(ctx, "You Selected" + p.name, Toast.LENGTH_SHORT).show(); 

      }else{ 
       SharedPreferences settings = ctx.getSharedPreferences("data", Context.MODE_PRIVATE); 
       settings.edit().putBoolean(p.name, false).commit(); 
       Toast.makeText(ctx, "You Deselected" +p.name, Toast.LENGTH_SHORT).show(); 

      } 
     } 
    });{ 

     return view; 

    } 

} 
+0

ListViewの代わりにRecyclerViewを使用すると、パフォーマンスとデータの永続性が向上します。 –

+0

@AbdenaceurLichiheb Im現時点で国家の節約を達成しようとしていますが、私はまだ一緒にAndroidでプログラミングする方法を学んでいます。 – 77rshah

答えて

1

フィールドは永続ストレージではありません。起動時にデータを永続化したい場合は、終了時にデータを保存し、開始時にデータをロードする必要があります。

+0

どうすれば実装できますか?例えば、 – 77rshah

+1

。 SharedPreferencesを使用してhttps://developer.android.com/training/basics/data-storage/shared-preferences.html – F43nd1r

+0

私はそれを見ていましたが、これを実装する方法はわかりません。 app/ – 77rshah

関連する問題