I've got a strange bug when I am clicking on checkbox (or selecting the item) trough a ListView.
Due to my custom base adapter I want to execute setItemChecked() everytime a checkbox is checked or not.
The first click do nothing (looks like it is not clicked), but I checked it whether onclick() was fired or not and it did. So it just looked not clicked.
The second click works apparently and the third as well.
But then it restarts and the next click does not change the look of the checkbox.
Furthermore if I click on other checkboxes in one of the "click-stages" the checkboxes will not behave like they should do..
Here is the important excerpt of my BaseAdapter Class with the functions:
final int finalPosition = position;
checkbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if (((CheckBox) arg0).isChecked()) {
((ListView)parent).setItemChecked(finalPosition, true);
} else {
((ListView)parent).setItemChecked(finalPosition, false);
}
}
});
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
checkbox.toggle();
if (checkbox.isChecked()) {
((ListView)parent).setItemChecked(finalPosition, true);
} else {
((ListView)parent).setItemChecked(finalPosition, false);
}
}
});
And this is the whole class:
public class ShoppingListBaseAdapter extends BaseAdapter {
private ArrayList<ShoppingListEntry> m_itemList;
private Context m_context;
private LayoutInflater m_inflater;
public ShoppingListBaseAdapter(Context context, ArrayList<ShoppingListEntry> shoppingListEntryArrayList) {
m_itemList = shoppingListEntryArrayList;
m_context = context;
m_inflater = (LayoutInflater.from(context));
}
@Override
public int getCount() {
return m_itemList.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, final ViewGroup parent) {
convertView = m_inflater.inflate(R.layout.list_row, null);
TextView title = (TextView) convertView.findViewById(R.id.txt_title);
TextView info = (TextView) convertView.findViewById(R.id.txt_info);
final CheckBox checkbox = (CheckBox) convertView.findViewById(R.id.checkbox1);
String itemTitle;
String productBrand = m_itemList.get(position).getProductBrand().toString();
if ( !productBrand.isEmpty() ) {
itemTitle = productBrand + " " + m_itemList.get(position).getProductName().toString();
} else {
itemTitle = m_itemList.get(position).getProductName().toString();
}
title.setText(itemTitle);
String itemInfo;
String productAmount = m_itemList.get(position).getProductAmount();
float productPrice = m_itemList.get(position).getProductPrice();
itemInfo = productAmount + ", " + productPrice + "€";
info.setText(itemInfo);
final int finalPosition = position;
checkbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if (((CheckBox) arg0).isChecked()) {
((ListView)parent).setItemChecked(finalPosition, true);
} else {
((ListView)parent).setItemChecked(finalPosition, false);
}
}
});
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
checkbox.toggle();
if (checkbox.isChecked()) {
((ListView)parent).setItemChecked(finalPosition, true);
} else {
((ListView)parent).setItemChecked(finalPosition, false);
}
}
});
return convertView;
}
What am I doing wrong?
Aucun commentaire:
Enregistrer un commentaire