问题
I have a listview with custom array adapter, the view allows the user to change a variable up and down with some plus and minus buttons I implemented
now the problem is that the plus/minus buttons will change the view ONCE, but after that time it won't update until the view is refreshed a different way. like using an listview.notifydatasetchanged() in an external operation.
when it updates, it shows that the plus/minus buttons WERE registering clicks and updating the variable, because on update of the data set it immediately puts the correct variable in the view
why isn't the individual view updating on its own?
private class MyListAdapter extends ArrayAdapter<Item> {
private ArrayList<Item> items;
TextView quantity;
int i;
public MyListAdapter(Context context, int textViewResourceId, ArrayList<Item> items) {
super(context, textViewResourceId, items);
this.items=items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_scanneditems, null);
}
final Item allItems = items.get(position);
//declare all parts of the cell
TextView product = (TextView)v.findViewById(R.id.productName);
quantity = (TextView)v.findViewById(R.id.listNum);
TextView category = (TextView)v.findViewById(R.id.categoryName);
TextView price = (TextView)v.findViewById(R.id.productCost);
product.setText(allItems.product);
category.setText(allItems.category);
price.setText("$" + allItems.price);
quantity.setText(String.valueOf(allItems.quantity)); //might want to do ceiling and floor functions here, IF weight not present
//set properties within on click listeners
//items.set(position, allItems.quantity);
ImageView selectLeft = (ImageView)v.findViewById(R.id.selectleft_list);
ImageView selectRight = (ImageView)v.findViewById(R.id.selectright_list);
CheckBox itemCheckBox = (CheckBox)v.findViewById(R.id.itemCheckBox);
itemCheckBox.setChecked(selectallListener);
itemCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked==false && selectallListener==true)
{
selectallListener=false;
CheckBox selectAll = (CheckBox)findViewById(R.id.selectAll);
selectAll.setChecked(false);
}
}
});
selectLeft.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(allItems.quantity > 0) //i greater than 0, decrement -1
{
allItems.setQuantity(allItems.quantity-1);
quantity.setText(String.valueOf(allItems.quantity));
}
else //i less or equal to 0
{
allItems.setQuantity(0);
quantity.setText(String.valueOf(allItems.quantity));
//turn icon into an X here an if it is X delete the item
}
}//onclick
});//selectleft listener
selectRight.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
allItems.setQuantity(allItems.quantity+1);
quantity.setText(String.valueOf(allItems.quantity));
}//onclick
});//selectRight listener
//do I need viewholder here?
//set listeners
return v;
}
回答1:
quantity variable could be unrelated to the current view in which clicks happens. Now it always is the TextView created/processed in last getView call.
I think must be like allItems variable. in getView function
final TextView quantity =…
来源:https://stackoverflow.com/questions/8109257/android-listview-view-cell-will-only-update-once