0

I have a list view, it has a textview and a image button(delete). When I click the image button the row gets deleted and the list view gets updated.

Everything is working fine, but the problem is when there are more that one row in the list view, after deleting one row, im not able to delete the other because the position value is not appropriate.

To put it clear, for eg: There are 3rows in the listview.

  • I clicked on the first row to delete( position value is 0), it gets deleted and the listview is updated.

so now there are two rows,

  • Again I clicked on 1st row and it failed to delete.(bcoz the position value is not getting updated with listview, actually now its position value should be 0, but instead its showing 1 and sometimes 2)

Here is my code,

CustomListView.class

public View getView(final int position, View convertView, ViewGroup parent) {
    holder = null;
    DataFields rowItems = (DataFields) getItem(position);
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.home_field_row, null);
        holder = new ViewHolder();
        holder.dataFields = items.get(position);
        holder.mName = (TextView) convertView
                .findViewById(R.id.hmFieldName);
        holder.mDeleteImage = (ImageView) convertView
                .findViewById(R.id.hmFieldDeleteImage);
        holder.mDeleteImage.setTag(position);

        holder.mDeleteImage
                .setOnClickListener(new ImageView.OnClickListener() {

                    @Override
                    public void onClick(final View v) {
                        Log.d("POSITION****", String.valueOf(position));
                        final Integer index = (Integer) v.getTag();

                        holder.dataFields = items.get(position);
                        value = holder.dataFields.getId();

                        int status = dbHandler
                                .deleteField(holder.dataFields);

                        if (status != 0) {
                            items.remove(index.intValue());
                            notifyDataSetChanged(); //ListView is getting updated but not the position values of rows
                        } else {
                            Toast.makeText(getContext(),
                                    "Failed to delete !",
                                    Toast.LENGTH_SHORT).show();

                        }
                    }
                });

        convertView.setTag(holder);
    }

    else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.mName.setText(rowItems.getName());

    return convertView;
}

Any kind of help is much appreciated. Thanks !

Edited

As you all said, I did the following changes but still the position value is not getting updated...

holder.dataFields = items.get(position);
DataFields obj = items.get(position);
value = holder.dataFields.getId();

int status = dbHandler.deleteField(holder.dataFields);
if (status != 0) {
    items.remove(index.intValue());
    HomeActivity.mAdapter.remove(obj); //I also tried items.remove(obj);
    HomeActivity.mAdapter.notifyDataSetChanged();
} else {
    Toast.makeText(getContext(), "Failed to delete !", Toast.LENGTH_SHORT).show();
}
0

5 Answers 5

1

after you do the delete part you should notify your adapter using notifyDataSetChanged()

Update

I think this is happening because you're using the view's tag as your index, this way your indexes never change. you should use the index provided by the getView method labeled final int position

Edit

can you try the following code instead of the one you posted as EDIT

int status = dbHandler.deleteField(items.get(position));
if (status != 0) {
    items.remove(position);
    notifyDataSetChanged();
} else {
    Toast.makeText(getContext(), "Failed to delete !", Toast.LENGTH_SHORT).show();
}
Sign up to request clarification or add additional context in comments.

5 Comments

I did that, see the comment line in my code. After removing item i have updated the listview using notifyDataSetChanged().
I didn't get what exactly you are trying to explain. Can you provide me the code line for it?
you're unsing final Integer index = (Integer) v.getTag(); and Then using items.remove(index.intValue()); why don't you use position instead of the tag?
Im not able to use that with postion, do you mean it should be like this "getPosition()" or getTag(position)?
ok let try one other thing.. try putting holder.mDeleteImage.setOnClickListener after holder.mName.setText(rowItems.getName());
0

Try this:

//Using the position we can
Object obj = adapter.getItem(position);
adapter.remove(obj);
//Notify the adapter
adapter.notifyDataSetChanged();

2 Comments

But that adapter is my MainActivity's class. How can I notify the same adapter in my CustomListView class??
0

I think it may be because you are deleting an item, but it's still in ViewHolder, so you should update your ViewHolder DataFields after deleting.

Comments

0

you have created DataFields class that holds records. So remove any object from that array list or hashmap (whatever you have used) and just write below code after deleting any object from list.

adapter.notifyDataSetChanged();

here, adapter is your custom adapter.

Edit

public class Contants extends Application
{
    public int xyz;
}

if you like to use this xyz object, you just simply need to write,

((Contants) getApplicationContext()).xyz;

1 Comment

But I cant directly use adapter of my MainActivity's class in my CustomListView's class rite?? Is there any way to do that?
0

Try resetting the adapter after the delete:

list.remove(adapter.getSelectedIndex());
reloadList();


    /**
 * Fixes bug where the list doesn't update. Call instead of just calling 
 * notifyDataSetChanged. 
 */
private void reloadList(){
    adapter.notifyDataSetChanged();
    getListView().setAdapter(adapter);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.