简单的购物清单应用程序。ListView(带有自定义适配器的TextView + CheckBox。试图实现viewHolder模式并完全丢失。对吗?请检查我的代码。还要如何保存复选框状态?我创建了大量但不知道如何实现的方法(滚动bug时取消选中)。
适配器:
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;
}
}
这是您的代码的正确实现-
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;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句