0
I own a Adapter
any and by clicking on an delete icon, the item in my ArrayList
is removed. The problem is that even by removing the desired item, the ListView
is updated, always removing the last element. The curious thing is that if I add a new item to the ArrayList
, the list is updated with the actual values.
Here is 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 3 {1,2,4} - Only, Listview displays {1,2,3}, even my Arraylist not containing 3, and containing 4.
Thank you! (:
Thank you so much! It was really what I wanted!
– Pedro Eduardo