Listview With Viewholder And Checkbox State Save
Simple shopping list app. ListView (TextView + CheckBox with custom adapter. Tried to implement viewHolder pattern and completely lost. Am i right? Check my code. And also how to s
Solution 1:
Here is the proper implementation of your code-
privatestaticclassViewHolder {
CheckBox checkBox;
TextView textView;
}
publicShopAdapter(Context mainContex, ArrayList<ShopItem> shopItems) {
this.mainContex = mainContex;
this.shopItems = shopItems;
}
@OverridepublicintgetCount() {
return shopItems.size();
}
@Overridepublic Object getItem(int position) {
return shopItems.get(position);
}
@OverridepubliclonggetItemId(int position) {
return position;
}
@Overridepublic View getView(int position, View convertView, ViewGroup parent) {
finalShopItemshopItem= shopItems.get(position);
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = newViewHolder();
LayoutInflaterlayoutInflater= 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(newCompoundButton.OnCheckedChangeListener() {
@OverridepublicvoidonCheckedChanged(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;
}
Solution 2:
See this implementation : (And for your checkbox .Do you want to retain checkbox state even after restarting app or just in scrolling ? )
// our ViewHolder.// caches our TextViewstaticclassViewHolderItem {
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 nullif(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;
}
Solution 3:
use SparseBooleanArray
Try below code
publicclassShopAdapterextendsBaseAdapter {
private Context mainContex;
private ArrayList<ShopItem> shopItems;
boolean[] checkBoxState = newboolean[shopItems.size()];
private SparseBooleanArray mSelectedItemsIds;
staticclassViewHolder {
CheckBox checkBox;
TextView textView;
}
publicShopAdapter(Context mainContex, ArrayList<ShopItem> shopItems) {
this.mainContex = mainContex;
this.shopItems = shopItems;
mSelectedItemsIds = newSparseBooleanArray();
}
@OverridepublicintgetCount() {
return shopItems.size();
}
@Overridepublic Object getItem(int position) {
return shopItems.get(position);
}
@OverridepubliclonggetItemId(int position) {
return position;
}
@Overridepublic View getView(int position, View convertView, ViewGroup parent) {
finalShopItemshopItem= shopItems.get(position);
Viewitem= convertView;
if (item == null) {
item = LayoutInflater.from(mainContex).inflate(R.layout.shoplist_item, null);
finalViewHolderviewHolder=newViewHolder();
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(newCompoundButton.OnCheckedChangeListener() {
@OverridepublicvoidonCheckedChanged(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));
}
ViewHolderholder= (ViewHolder) item.getTag();
holder.textView.setText(shopItems.get(position).getDescription());
holder.checkBox.setChecked(shopItems.get(position).isDone());
return item;
}
}
Post a Comment for "Listview With Viewholder And Checkbox State Save"