Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove an item and refresh the listview

Tags:

android

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;
    }
like image 269
Manikandan Avatar asked Jul 20 '12 08:07

Manikandan


People also ask

How can you update a ListView dynamically?

This example demonstrates how do I dynamically update a ListView in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do I remove all items from a list view?

ListView operates based on the underlying data in the Adapter . In order to clear the ListView you need to do two things: Clear the data that you set from adapter. Refresh the view by calling notifyDataSetChanged.

How do I refresh the list adapter?

To refresh the ListView in Android, call notifyDataSetChanged() method on the Adapter that has been set with the ListView. Also note that the method notifyDataSetChanged() has to be called on UI thread.

What is the use of ListView explain list view with example?

Android ListView is a ViewGroup that is used to display the list of items in multiple rows and contains an adapter that automatically inserts the items into the list. The main purpose of the adapter is to fetch data from an array or database and insert each item that placed into the list for the desired result.


1 Answers

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();
like image 115
AITAALI_ABDERRAHMANE Avatar answered Oct 22 '22 23:10

AITAALI_ABDERRAHMANE