• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps get items back from Listview

motoko

Newbie
hello, I have a listview thats is populated through from an SQLDatabase through a custom cursor adaptor. I would like to go back to the activity that i used to create the items when i click on them in the listview.(edit the items basically). i can go back using an intent, but i don't know how to populate the activity with the already entered data? Hope i am clear enough. Thank you
 
You can store any object with a view using the setTag method, so you can store an Integer that contains the primary key with each item in the ListView. When the item is tapped, use getTag to return the Integer and pass it as an extra in the intent (the putExtra method of the intent). In the new Activity, call Integer rowId = getIntent().getData(); Check if rowId != null and load it from the database.
 
Thank you very much. will try it, immediately i get back home.. just wanted to know, i am using a view holder to contain the textviews in my bindView() and am calling setTag on the holder. is it the same thing as you said, if i just make my view Holder to contain the primary key as well?
Thanks once again.
 
I'm not sure what you mean by "view holder" but any class that extends the View class can do setTag/getTag. As long as you can identify the view that was tapped, you can recover the primary key and pass it to the next activity.
 
i meant the viewHolder method from this video by romain guy, youtube.com/watch?v=N6YdwzAvwOA

sorry if i didn't say it correctly, but thanks all the same..
 
It looks like that ViewHolder is to store some View IDs for quick references, so it's fill the View's tag. Another option would be to @Override the getItemId method of the CursorAdapter so that can return the row ID. Then it should be set for you when your OnItemClickListener's onItemClick method gets called (as the fourth param).
 
hello, i tried the OnItemClickListener but nothing seems to happen. it does not go back to my intended class. This is my code. i don't know if am wrong or i did not override the getItemId() correctly. Please correct me if am wrong:

Code:
public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
			
			Cursor c = adapter.retrieveRow(rowId);
			//c.moveToPosition(position);
			Intent edit = new Intent(this,NewItem.class);
				edit.putExtra(DBAdapter.KEY_ID, rowId);
				edit.putExtra(DBAdapter.NAME, c.getString(c.getColumnIndex(DBAdapter.NAME)));
				edit.putExtra(DBAdapter.START_DATE, c.getString(c.getColumnIndex(DBAdapter.START_DATE)));
				startActivity(edit);
	
	}
Thank you.
 
Is the surrounding code for the snippet you posted something like this:

Code:
getListView().setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        // stuff here
    }
});

If so, I'd first add a Log.d call in there to verify the onItemClick method is being triggered.
 
oh sorry about that, i should have updated the post earlier. I forgot to do setOnItemClickListener() on the ListView. the problem am having now is that when the focused view is clicked, the entry before it is returned.
 
Back
Top Bottom