简体   繁体   中英

Edit List Items in ListView

I have a listview from an SQLDatabase using custom CursorAdaptor. I would like to go back to the activity that i used to create the items when i click on them in the listview.so that i can edit the entries. But nothing happen when i implement the OnItemClick method and the getItemId() on the CursorAdapter even though am not sure i am correct there. Here is my Code:

public void onItemClick(AdapterView<?> adapview, View view, int position, long rowId) {

        Cursor c = adapter.retrieveRow(rowId); // retrieve row from Database
            Intent edit = new Intent(this,NewItem.class);
        edit.putExtra(DBAdapter.KEY_ID, rowId);
            edit.putExtra(DBAdapter.Title, c.getString(c.getColumnIndex(DBAdapter.Title)));
      edit.putExtra(DBAdapter.DATE, c.getString(c.getColumnIndex(DBAdapter.DATE)));
            startActivity(edit);
        }

public long getItemId(int id){
            return id;
        }

I think you need to set an onItemClickListener on the Listview rather than trying to implement it in the Adapter class.

EXAMPLE:

mListView = (ListView)findViewbyId(R.id.whatever)
mListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View view, int position,
                    long rowId) {
                Cursor c = adapter.retrieveRow(rowId); // retrieve row from Database
                Intent edit = new Intent(this,NewItem.class);
                edit.putExtra(DBAdapter.KEY_ID, rowId);
                edit.putExtra(DBAdapter.Title, c.getString(c.getColumnIndex(DBAdapter.Title)));
                edit.putExtra(DBAdapter.DATE, c.getString(c.getColumnIndex(DBAdapter.DATE)));
                startActivity(edit);
            }
        });

Is this what you're doing?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM