Srujan Simha Adicharla
Srujan Simha Adicharla

Reputation: 3723

Delete row in custom ListView

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.

so now there are two rows,

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();
}

Upvotes: 0

Views: 2284

Answers (5)

Ben
Ben

Reputation: 1

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);
}

Upvotes: 0

A. AMAIDI
A. AMAIDI

Reputation: 6849

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();
}

Upvotes: 1

Chintan Rathod
Chintan Rathod

Reputation: 26034

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;

Upvotes: 0

Michał Z.
Michał Z.

Reputation: 4119

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.

Upvotes: 0

user1744952
user1744952

Reputation: 518

Try this:

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

Upvotes: 0

Related Questions