get items back from Listview


Last Updated:

  1. motoko

    motoko Member This Topic's Starter

    Joined:
    Jun 26, 2010
    Messages:
    11
    Likes Received:
    0
    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
     

    Advertisement
  2. IanGClifton

    IanGClifton Well-Known Member

    Joined:
    Feb 7, 2010
    Messages:
    111
    Likes Received:
    19
    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.
     
    motoko likes this.
  3. motoko

    motoko Member This Topic's Starter

    Joined:
    Jun 26, 2010
    Messages:
    11
    Likes Received:
    0
    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.
     
  4. IanGClifton

    IanGClifton Well-Known Member

    Joined:
    Feb 7, 2010
    Messages:
    111
    Likes Received:
    19
    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.
     
  5. motoko

    motoko Member This Topic's Starter

    Joined:
    Jun 26, 2010
    Messages:
    11
    Likes Received:
    0
    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..
     
  6. IanGClifton

    IanGClifton Well-Known Member

    Joined:
    Feb 7, 2010
    Messages:
    111
    Likes Received:
    19
    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).
     
  7. motoko

    motoko Member This Topic's Starter

    Joined:
    Jun 26, 2010
    Messages:
    11
    Likes Received:
    0
    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 (Text):
    1. public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
    2.            
    3.             Cursor c = adapter.retrieveRow(rowId);
    4.             //c.moveToPosition(position);
    5.             Intent edit = new Intent(this,NewItem.class);
    6.                 edit.putExtra(DBAdapter.KEY_ID, rowId);
    7.                 edit.putExtra(DBAdapter.NAME, c.getString(c.getColumnIndex(DBAdapter.NAME)));
    8.                 edit.putExtra(DBAdapter.START_DATE, c.getString(c.getColumnIndex(DBAdapter.START_DATE)));
    9.                 startActivity(edit);
    10.    
    11.     }
    Thank you.
     
  8. IanGClifton

    IanGClifton Well-Known Member

    Joined:
    Feb 7, 2010
    Messages:
    111
    Likes Received:
    19
    Is the surrounding code for the snippet you posted something like this:

    Code (Text):
    1. getListView().setOnItemClickListener(new OnItemClickListener() {
    2.     @Override
    3.     public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    4.         // stuff here
    5.     }
    6. });
    7.  
    If so, I'd first add a Log.d call in there to verify the onItemClick method is being triggered.
     
  9. motoko

    motoko Member This Topic's Starter

    Joined:
    Jun 26, 2010
    Messages:
    11
    Likes Received:
    0
    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.
     

Share This Page

Loading...