Manikandan
Manikandan

Reputation: 1832

How to remove an item and refresh the listview

I have an listview in my program. In that i populated the values from the database and displayed. Iam using context menu to edit,delete items in listview. if i chose delete in the context menu it deletes the item in the database but i did'nt removed the item from the listview. What iam doing wrong here. Please help me if anybody knows.

Retrieve method:

public void retrievedb() {
        strquery = "SELECT * FROM api_settings";
        Cursor mCursor = (MainscreenActivity.JEEMAAndroSMSDB).rawQuery(
                strquery, null);
        if (mCursor.getCount() != 0) {
            mCursor.moveToLast();
            for (int i = mCursor.getCount() - 1; i >= 0; i--) {
                dbid = mCursor.getString(0);
                dbapiname = mCursor.getString(1);
                dbURL = mCursor.getString(2);
                dbUnameVar = mCursor.getString(3);
                dbUnameVal = mCursor.getString(4);
                dbPwdVar = mCursor.getString(5);
                dbPwdVal = mCursor.getString(6);
                dbsendername = mCursor.getString(7);
                dbdestinationvar = mCursor.getString(8);
                dbmsgvariable = mCursor.getString(9);
                dbchars = mCursor.getString(10);
            }
        }

        if ((dbapiname == null) || (dbURL == null) || (dbUnameVar == null)
                || (dbUnameVal == null) || (dbPwdVar == null)
                || (dbPwdVal == null) || (dbsendername == null)
                || (dbdestinationvar == null) || (dbmsgvariable == null)
                || (dbchars == null)) {

            tvText = (TextView) findViewById(android.R.id.text1);
            tvText.setVisibility(1);
        } else {
            adapter = new ArrayAdapter<Settingsmodel>(this,
                    android.R.layout.simple_list_item_1, listItems);
            tvText = (TextView) findViewById(android.R.id.text1);
            tvText.setVisibility(View.GONE);
            strquery = "SELECT ID,API_Name FROM api_settings";
            mCursor = (MainscreenActivity.JEEMAAndroSMSDB).rawQuery(strquery,
                    null);
            if (mCursor.getCount() != 0) {
                mCursor.moveToFirst();
                do {
                    dbid = mCursor.getString(0);
                    dbapiname = mCursor.getString(1);
                    listItems.add(new Settingsmodel(dbapiname, dbid));
                    adapter.notifyDataSetChanged();
                } while (mCursor.moveToNext());
            }
        }
    }

Context Menu:

@Override
    public void onCreateContextMenu(ContextMenu menu, final View v,
            ContextMenuInfo menuInfo) {
        if (v.getId() == android.R.id.list) {
            AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
            Settingsmodel selectedValue = (Settingsmodel) getListAdapter()
                    .getItem(info.position);
            String tempo = selectedValue.getSpinnerText();
            menu.setHeaderTitle(tempo);
            String[] menuItems = getResources().getStringArray(R.array.menu);
            for (int i = 0; i < menuItems.length; i++) {
                menu.add(Menu.NONE, i, i, menuItems[i]);
            }

        }
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
                .getMenuInfo();
        int menuItemIndex = item.getItemId();
        String[] menuItems = getResources().getStringArray(R.array.menu);
        String menuItemName = menuItems[menuItemIndex];
        Settingsmodel selectedValue = (Settingsmodel) getListAdapter().getItem(
                info.position);
        String temp0 = selectedValue.getSpinnerText();
        String temp1 = selectedValue.getValue();
        int id = Integer.parseInt(temp1);
        if (menuItemName.equalsIgnoreCase("Set as Default")) {
            ContentValues values = new ContentValues();
            values.put(MainscreenActivity.COL_Set_Default, value);
            MainscreenActivity.JEEMAAndroSMSDB.update(
                    MainscreenActivity.TABLE_Name, values, "ID=" + id, null);
            values.put(MainscreenActivity.COL_Set_Default, 0);
            Toast.makeText(getBaseContext(), temp0 + " is set as Default",
                    Toast.LENGTH_LONG).show();
            MainscreenActivity.JEEMAAndroSMSDB.update(
                    MainscreenActivity.TABLE_Name, values, "ID!=" + id, null);
        }

        if (menuItemName.equalsIgnoreCase("Delete")) {
            MainscreenActivity.JEEMAAndroSMSDB.delete(
                    MainscreenActivity.TABLE_Name, "ID=" + id, null);           
            Toast.makeText(getBaseContext(), "API Deleted Successfully",
                    Toast.LENGTH_SHORT).show(); 
            retrievedb();
        }

        return true;
    }

Upvotes: 5

Views: 9373

Answers (5)

AITAALI_ABDERRAHMANE
AITAALI_ABDERRAHMANE

Reputation: 2519

You can refresh your List after deleting item by

YourListView.setAdapter(YourAdapter); // optional

YourAdapter.notifyDataSetChanged(); // mandatory

enjoy

Edit:

Best way: (the first solution is not harming)

listItems.remove(position);
listViewAdapter.notifyDataSetChanged();

Upvotes: 6

Tang Ke
Tang Ke

Reputation: 1508

try to remove the item from listItems(List), then call

adapter.notifyDataSetChanged();

Upvotes: 4

pankaj716
pankaj716

Reputation: 59

First remove an item from arraylist from which you populating list to your listview by

arraylist.remove(position);

then call

listview.invalidateViews();

Upvotes: 1

Yogesh Somani
Yogesh Somani

Reputation: 2624

Have you tried doing it on the UI thread :

  listview.remove(position);
        runOnUiThread(new Runnable() {
            public void run() {
                adapter.notifyDataSetChanged();
            }
        });

This may help.

Upvotes: 1

Ram kiran Pachigolla
Ram kiran Pachigolla

Reputation: 21191

There are two methods for this

1) Once after your Item from list view call the methos adapter.notifyChanged();

                            (or) 
2) create a method that append data to the listview, after delete the record call that method again. its just another way of refreshing listview 

Upvotes: 2

Related Questions