Reputation: 4408
I have a listview with each row containing some text and a delete button. When delete button is pressed the row has to be deleted.
I am using a base adapter and there is a global list the item of which will be displayed.
Please see my adpater code below
public class JobCartListAdapter extends BaseAdapter {
protected LayoutInflater mInflater;
public JobCartListAdapter( Context mContext) {
super();
this.mContext = mContext;
mInflater = LayoutInflater.from(mContext);
}
Context mContext;
@Override
public int getCount() {
// TODO Auto-generated method stub
return JobsManager.JobsCartList.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.job_cart_row, null);
holder = new ViewHolder();
holder.text1 = (TextView) convertView.findViewById(R.id.title);
holder.delete = (ImageButton)convertView.findViewById(R.id.delete);
holder.delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i("Remove from" , "" + position);
JobsManager.JobsCartList.remove(position) ;
notifyDataSetChanged();
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text1.setText(JobsManager.JobsCartList.get(position).getTitle());
return convertView;
}
static class ViewHolder {
TextView text1;
ImageButton delete;
}
}
the issue i am facing is :
when i press delet for the firstime , the position within the list is correct and the item is deleted and list is getting refreshed
when i click again on a row in the refreshed list , the corresponding position passed to getview is not right . So its leading to an index out of bound exception as that position is used as index for list . I checked my list and it is correctly updated on add and remove . Its the problem with list adapter. After refresh of the list with call of notifystatechanged, when a row is clicked, the postion of the row returned is not correct.
please help
Upvotes: 0
Views: 1980
Reputation: 4340
You don't have to remove the View just remove the relates object by adapter.remove(adapter.getItem(position));
it will remove the specified object from list & call the the method notifyDatasetChanged()
Upvotes: 1