Skip to content Skip to sidebar Skip to footer

Listview Refresh After Removing Some Items

Good day everyone. I want the ListView refresh after removing some items from database but I'm seeing a red lines underneath remove (cannot be resolved). I am having below code. p

Solution 1:

With this (not compiling) line, you are trying to manipulate the list view:

p.remove(p.getItemAtPosition(po));

That's not how it works. Don't try to manipulate the view directly. The idea of using an adapter to provide data for the view is that you manipulate the data, not the view, and then signal that something has changed. The trick is knowing how to signal. It seems your next line does exactly that:

dataAdapter.notifyDataSetChanged();

So delete the first line (with p.remove), and it might actually work.

If that's not enough (though I think it should be) then you might need to swap the cursor in the adapter with a new one, like this:

Cursorcursor2= ... // create the same way you did when you created SimpleCursorAdapter
dataAdapter.changeCursor(cursor2);

In your updated code:

builder.setPositiveButton("Yes", newDialogInterface.OnClickListener() {
    publicvoidonClick(DialogInterface dialog, int ii) {

        database = dbHelper.getWritableDatabase();
        Cursorcursor= (Cursor) p.getItemAtPosition(po);

        // Get the state's capital from this row in the database.longID=
                cursor.getLong(cursor.getColumnIndexOrThrow("_id"));
        sqlcon.delete(ID);

        Cursorcursor2= sqlcon.readEntry(name1);
        dataAdapter.changeCursor(cursor2);
    }
});

Solution 2:

this is what i have done to delete list row try something like this in your adapter class

delet.setOnClickListener(newView.OnClickListener() {
 @OverridepublicvoidonClick(View view) {
        cartdata.updateSelected(Integer.parseInt(cart_pdiscription[i]), 0);
        cartdata.deleteProduct(Integer.parseInt(cart_pdiscription[i]));                
        Intent intent = ((CartList) context).getIntent();
        ((CartList) context).finish();
        context.startActivity(intent);

    }
});

Solution 3:

I'm seeing a red lines underneath remove (cannot be resolved)

this happens because the class AdapterViewdoes not have a method called remove. Refer these link and this too.

do code to delete row of the selected Id as in your edit,

     database =   dbHelper.getWritableDatabase();
                            Cursorcursor= (Cursor) p.getItemAtPosition(po);

                            // Get the state's capital from this row in the database.longID=
                                    cursor.getLong(cursor.getColumnIndexOrThrow("_id"));
                          sqlcon.delete(ID);                          

                        }
                    });
and then trythis snippet

    cursor=sqlcon.readEntry(name1);
            dataAdapter = newSimpleCursorAdapter(
                    this, R.layout.listdispaly,
                    cursor,
                    columns,
                    to, 0);
    //listView.setAdapter(null);
    listView.setAdapter(dataAdapter);

But when I long press the row and the AlertDialog pop out, all the text become white .Why would this happen?

I dont know why it happens but this snippet can help you to get rid of the problem

builder.setInverseBackgroundForced(true);
AlertDialogdialog= builder.create();
dialog.show();

It will force the background color to be inverted. This flag is only used for pre-Material themes.This method was deprecated in API level 23. This flag is only used for pre-Material themes. Instead, specify the window background using on the alert dialog theme.

Post a Comment for "Listview Refresh After Removing Some Items"