2017-05-27 67 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

使用RecyclerView而不是ListView在性能和數據持久性方面更好。 –

+0

@ AbdenaceurLichiheb我試圖實現當前的狀態拯救,即時通訊仍然在學習如何在Android上編程。 – 77rshah

回答

1

字段不持久存儲。如果您想在啓動時保留數據,則必須將其保存在關閉狀態並在開始時加載。

+0

我將如何實現? – 77rshah

+1

例如使用SharedPreferences https://developer.android.com/training/basics/data-storage/shared-preferences.html – F43nd1r

+0

好吧,我看了一下,但我不知道如何實現這一點,它的工作退出應用程序和加載備份的應用程序/ – 77rshah