I've got a listview containing checkbox, text ...
My problem is with the checkbox, i've got a list of Objects (models) that are coming from my local database, i've got a boolean in my model which is telling me if i need to check or not the checkbox at the moment i build the listview. My problem is from the moment i check one box, the next time i'm coming to that view, all the checkbox will be checked, or unchecked ...
I first thaught that i had a problem with my list of models, but they are all correct ... i have no idea why ...
Here is the getView method of the adapter:
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder allItems = null;
if (convertView == null)
{
allItems = new ViewHolder();
convertView = mInflater.inflate(R.layout.row, null);
allItems.delete = (ImageButton) convertView.findViewById(R.id.deleteItem);
allItems.chkTick = (CheckBox) convertView.findViewById(R.id.checkBox1);
allItems.txtName = (TextView) convertView.findViewById(R.id.nameItem);
allItems.price = (TextView) convertView.findViewById(R.id.priceItem);
allItems.numberItem = (TextView) convertView.findViewById(R.id.numberItem);
allItems.plus = (ImageButton) convertView.findViewById(R.id.plusItem);
allItems.minus = (ImageButton) convertView.findViewById(R.id.minusItem);
convertView.setTag(allItems);
}
else
allItems = (ViewHolder) convertView.getTag();
final int pos = position;
allItems.txtName.setText(this._sqlHelper.getItemById(items.get(position).getItemId()).getName());
allItems.numberItem.setText(Integer.toString(items.get(position).getQuantity()));
allItems.chkTick.setChecked(items.get(position).getIsChecked());
if (tmp != null && tmp.compareTo("TRUE") == 0)
{
allItems.price.setVisibility(View.VISIBLE);
allItems.price.setText("(" + Double.toString(items.get(position).getPrice()) + "€)");
}
else
allItems.price.setVisibility(View.INVISIBLE);
allItems.chkTick.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
items.get(pos).setIsChecked(isChecked);
getPos(buttonView);
if (isChecked)
{
if (tmp != null && tmp.compareTo("TRUE") == 0)
ShowPopup(activity, p, pos);
}
_sqlHelper.updateShoppingListItem(items.get(pos));
}
});
allItems.delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(ListItemsAdaptor.this.activity);
builder.setCancelable(false);
builder.setTitle("Delete Item");
builder.setMessage("Are you sure you want to delete this item ?");
builder.setPositiveButton("Update", null);
builder.setNegativeButton("Cancel", null);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
_sqlHelper.deleteShoppingListItemByIdItem(items.get(pos).getListId(), items.get(pos).getItemId());
items.remove(pos);
updateResults(items);
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
builder.show();
}
});
allItems.plus.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
items.get(pos).setQuantity(items.get(pos).getQuantity()+ 1);
updateResults(items);
_sqlHelper.updateShoppingListItem(items.get(pos));
}
});
allItems.minus.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (items.get(pos).getQuantity() > 1){
items.get(pos).setQuantity(items.get(pos).getQuantity()- 1);
updateResults(items);
_sqlHelper.updateShoppingListItem(items.get(pos));
}
}
});
return (convertView);
}
Thank you for helping guys !
我建议您使用私有ListArray的checkedItems并在选中的项中添加或删除,毕竟在代码之后传递或使用此列表Array。
我认为您需要在onCheckedChangeListener中(以及每次更改项目之后notifyDataSetChanged()
调用适配器的notifyDataSetChanged()
方法。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.