[英]ListView with viewholder and checkbox state save
簡單的購物清單應用程序。 ListView(帶有自定義適配器的TextView + CheckBox。試圖實現viewHolder模式並完全丟失。對嗎?檢查我的代碼。還有如何保存復選框狀態?我創建了大量但不知道如何實現的方法(滾動錯誤時取消選中)。
適配器:
public class ShopAdapter extends BaseAdapter {
private Context mainContex;
private ArrayList<ShopItem> shopItems;
boolean[] checkBoxState = new boolean[shopItems.size()];
static class ViewHolder {
CheckBox checkBox;
TextView textView;
}
public ShopAdapter(Context mainContex, ArrayList<ShopItem> shopItems) {
this.mainContex = mainContex;
this.shopItems = shopItems;
}
@Override
public int getCount() {
return shopItems.size();
}
@Override
public Object getItem(int position) {
return shopItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ShopItem shopItem = shopItems.get(position);
View item = convertView;
if (item == null) {
item = LayoutInflater.from(mainContex).inflate(R.layout.shoplist_item, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.textView = (TextView) item.findViewById(R.id.itemTextView);
viewHolder.checkBox = (CheckBox) item.findViewById(R.id.doneCheckBox);
viewHolder.textView.setText(shopItem.getDescription());
viewHolder.checkBox.setChecked(shopItem.isDone());
viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
shopItem.setDone(true);
viewHolder.textView.setTextColor(mainContex.getResources()
.getColor(R.color.done_text_color));
} else {
shopItem.setDone(false);
viewHolder.textView.setTextColor(mainContex.getResources()
.getColor(R.color.secondary_text));
}
}
});
item.setTag(viewHolder);
viewHolder.checkBox.setTag(shopItems.get(position));
} else {
item = convertView;
((ViewHolder) item.getTag()).checkBox.setTag(shopItems.get(position));
}
ViewHolder holder = (ViewHolder) item.getTag();
holder.textView.setText(shopItems.get(position).getDescription());
holder.checkBox.setChecked(shopItems.get(position).isDone());
return item;
}
}
項目:
public class ShopItem {
private String description;
private boolean done;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public boolean isDone() {
return done;
}
public void setDone(boolean done) {
this.done = done;
}
}
請參見此實現:(對於您的復選框。是否要在重新啟動應用程序后或僅在滾動狀態下保留復選框狀態?)
// our ViewHolder.
// caches our TextView
static class ViewHolderItem {
TextView textViewItem;
}
getView()
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolderItem viewHolder;
/*
* The convertView argument is essentially a "ScrapView".
* It will have a non-null value when ListView is asking you recycle the row layout.
* So, when convertView is not null, you should simply update its contents instead of inflating a new row layout.
*/
if(convertView==null){
// inflate the layout
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(layoutResourceId, parent, false);
// well set up the ViewHolder
viewHolder = new ViewHolderItem();
viewHolder.textViewItem = (TextView) convertView.findViewById(R.id.textViewItem);
// store the holder with the view.
convertView.setTag(viewHolder);
}else{
// we've just avoided calling findViewById() on resource everytime
// just use the viewHolder
viewHolder = (ViewHolderItem) convertView.getTag();
}
// object item based on the position
ObjectItem objectItem = data[position];
// assign values if the object is not null
if(objectItem != null) {
// get the TextView from the ViewHolder and then set the text (item name) and tag (item ID) values
viewHolder.textViewItem.setText(objectItem.itemName);
viewHolder.textViewItem.setTag(objectItem.itemId);
}
return convertView;
}
這是您的代碼的正確實現-
private static class ViewHolder {
CheckBox checkBox;
TextView textView;
}
public ShopAdapter(Context mainContex, ArrayList<ShopItem> shopItems) {
this.mainContex = mainContex;
this.shopItems = shopItems;
}
@Override
public int getCount() {
return shopItems.size();
}
@Override
public Object getItem(int position) {
return shopItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ShopItem shopItem = shopItems.get(position);
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater layoutInflater = LayoutInflater.from(mContext);
convertView = layoutInflater.inflate(R.layout.shoplist_item, null);
viewHolder.textView = (TextView) item.findViewById(R.id.itemTextView);
viewHolder.checkBox = (CheckBox) item.findViewById(R.id.doneCheckBox);
convertView.setTag(viewHolder);
}
else
{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.checkBox.setTag(shopItems.get(position));
viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
shopItem.setDone(true);
viewHolder.textView.setTextColor(mainContex.getResources()
.getColor(R.color.done_text_color));
} else {
shopItem.setDone(false);
viewHolder.textView.setTextColor(mainContex.getResources()
.getColor(R.color.secondary_text));
}
}
});
viewHolder.textView.setText(shopItem.getDescription());
viewHolder.checkBox.setChecked(shopItem.isDone());
return convertView;
}
使用SparseBooleanArray
嘗試下面的代碼
public class ShopAdapter extends BaseAdapter {
private Context mainContex;
private ArrayList<ShopItem> shopItems;
boolean[] checkBoxState = new boolean[shopItems.size()];
private SparseBooleanArray mSelectedItemsIds;
static class ViewHolder {
CheckBox checkBox;
TextView textView;
}
public ShopAdapter(Context mainContex, ArrayList<ShopItem> shopItems) {
this.mainContex = mainContex;
this.shopItems = shopItems;
mSelectedItemsIds = new SparseBooleanArray();
}
@Override
public int getCount() {
return shopItems.size();
}
@Override
public Object getItem(int position) {
return shopItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ShopItem shopItem = shopItems.get(position);
View item = convertView;
if (item == null) {
item = LayoutInflater.from(mainContex).inflate(R.layout.shoplist_item, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.textView = (TextView) item.findViewById(R.id.itemTextView);
viewHolder.checkBox = (CheckBox) item.findViewById(R.id.doneCheckBox);
if(mSelectedItemsIds.get(position)){
shopItem.setDone(true);
viewHolder.textView.setTextColor(mainContex.getResources().getColor(R.color.done_text_color));
} else{
shopItem.setDone(false);
viewHolder.textView.setTextColor(mainContex.getResources().getColor(R.color.secondary_text));
}
viewHolder.textView.setText(shopItem.getDescription());
viewHolder.checkBox.setChecked(shopItem.isDone());
viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
mSelectedItemsIds.put(position, value);
shopItem.setDone(true);
viewHolder.textView.setTextColor(mainContex.getResources()
.getColor(R.color.done_text_color));
} else {
mSelectedItemsIds.delete(position);
shopItem.setDone(false);
viewHolder.textView.setTextColor(mainContex.getResources()
.getColor(R.color.secondary_text));
}
}
});
item.setTag(viewHolder);
viewHolder.checkBox.setTag(shopItems.get(position));
} else {
item = convertView;
((ViewHolder) item.getTag()).checkBox.setTag(shopItems.get(position));
}
ViewHolder holder = (ViewHolder) item.getTag();
holder.textView.setText(shopItems.get(position).getDescription());
holder.checkBox.setChecked(shopItems.get(position).isDone());
return item;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.