I have Adapter
any, and when I click on an exclude icon, the item of my ArrayList
is removed. The problem is that even removing the desired item, ListView
is updated, always removing the last element. The curious thing is that if I add a new item to ArrayList
, the list is updated with the actual values.
Here's my code:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//final ViewHolder holder;
View v = convertView;
final int pos = position;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.adapter_product_list, null);
//holder = new ViewHolder();
if (productList.size() > 0) {
//Product name
final Product product = productList.get(position);
final TextView productName = (TextView) v.findViewById(R.id.productName);
productName.setEnabled(false);
//Edit Button
ImageButton editButton = (ImageButton) v.findViewById(R.id.editButton);
//Delete button
final ImageButton deleteButton = (ImageButton) v.findViewById(R.id.deleteButton);
deleteButton.setTag(position);
try {
if (productName != null)
productName.setText(String.valueOf(product.getDescription()));
} catch (Exception e) {
e.printStackTrace();
}
//Listeners
editButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
productName.setEnabled(true);
productName.requestFocus();
}
});
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(view.getRootView().getContext());
Integer tag = (Integer) view.getTag();
newDialogBuilder(tag);
notifyDataSetChanged();
String yes = context.getResources().getString(R.string.yes);
String no = context.getResources().getString(R.string.no);
builder.setMessage(context.getResources().getString(R.string.areYouSureDelete))
.setPositiveButton(yes, dialogDelete)
.setNegativeButton(no, dialogDelete).show();
}
});
}
}
return v;
}
private void newDialogBuilder(final int position) {
this.dialogDelete = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case DialogInterface.BUTTON_POSITIVE:
productList.remove(position);
ProductListAdapter.this.notifyDataSetChanged();
break;
case DialogInterface.BUTTON_NEGATIVE:
break;
}
ProductListAdapter.this.notifyDataSetChanged();
}
};
}
Example: - My list has {1,2,3,4} - Remove the 3 {1,2,4} - Only, the ListView displays {1,2,3}, even my ArrayList does not contain 3, and contains the 4.
Thank you! (: