1

I am pretty new to android programming, hence can't solve many things on my own. For last few hours, I was trying to write code for doing some activity based on user click on a list view. Basically, I need to do below:

1) I already have successfully generated a listview with BOOK ID, BOOK NAME and WRITER information 2) If I click on each row, I want to read the BOOK ID of that row, go back to Main Page and then run a predefined function x(BOOK_ID) on main page.

I tried to search a lot, but being a novice, everything is just making me more confused. Any help will be much appreciated.

Single Block for Listview:

    <TextView
        android:id="@+id/bookname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Book Title" />
    <TextView
        android:id="@+id/bookid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Book ID" />

    <TextView
        android:id="@+id/writer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Writer" />
</RelativeLayout>

Java Class:

mylibmandbhandler db = new mylibmandbhandler(this);
        List<mylibman> allfld = db.getAllRecord();
        ArrayList<HashMap<String, String>> allbookList = new ArrayList<HashMap<String, String>>();
        for (mylibman cn : allfld) {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put(LIST_KEY_ID, Integer.toString(cn.getBookid()));
            map.put(LIST_KEY_NAME, cn.getBookname());
            map.put(LIST_KEY_WRITER, cn.getWriter());
            allbookList.add(map);
        }
        list=(ListView)findViewById(R.id.list);
        adapter=new MylibmanList(this, allbookList); //calling adapter class
        list.setAdapter(adapter);
// UPTO THIS POINT CODE IS WORKING FINE

        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// NEED TO GET THE BOOKID OF THE ROW UPON CLICKING, GO BACK TO MAIN ACTIVITY, AND CALL A FUNCTION X()
            }
        });
abdfahim
  • 2,523
  • 10
  • 35
  • 71
  • 1
    `getItem(position)` Implement a method `getItem(int position)` in your ArrayAdapter which return the HashMap of a specific Listitem like `return allbooks.get(positions)` – A.S. Nov 09 '13 at 12:26

3 Answers3

1

in your Adapter class, implement getItem(int position) like this...

@Override
public Object getItem(int position) {
  return allBookList.get(position);
}

and implement OnItemClickListener like this..

 list.setOnItemClickListener(new OnItemClickListener() {
     @Override
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        HashMap<String, String> hashMap = (HashMap<String, String>) parent.getItemAtPosition(position);
        String string = hashMap.get(LIST_KEY_ID);
        int id = Integer.valueOf(string);
        Intent intent = new Intent();
        intent.putExtra("bookId",id);
        setResult(RESULT_OK,intent);
        finish();
     }
 });

and how to start the Activity for Result, see the link given by 2Dee

Community
  • 1
  • 1
Gopal Gopi
  • 11,101
  • 1
  • 30
  • 43
0

To retrieve a reference to an object in a ListView, you can use the get method from the Adapter inside you OnItemClick :

String bookId = adapter.get(position).get(LIST_KEY_ID);
// then return to your MainActivity, using the startActivityForResult mechanism
Intent returnIntent = new Intent();
returnIntent.putExtra("bookId",bookId);
setResult(RESULT_OK,returnIntent);     
finish();

More details on using startActivityForResult here.

Community
  • 1
  • 1
2Dee
  • 8,609
  • 7
  • 42
  • 53
  • can you please elaborate the purpose of setResult(RESULT_OK,returnIntent); – abdfahim Nov 09 '13 at 16:36
  • nevermind, I got the explanation :) ... giving a try – abdfahim Nov 09 '13 at 16:43
  • RESULT_OK is the result code used to advise your MainActivity that the action it was expecting from the Activity you started was successful. returnIntent in the Intent sent back to MainActivity containing the data you want to send back as the result of your request. More details in the link I provided or at the official docs : http://developer.android.com/reference/android/app/Activity.html (look for Starting Activities and Getting Results. – 2Dee Nov 09 '13 at 16:44
  • thanks, I just figured it out ... thanks for details though ... btw, I just followed your path and implemented it with success, thanks a lot !! Just one last question in this regard for my knowledge. I see everywhere people use two methods getItem() and getItemId() in adapter class. Which function should I use in this case (currently I used getItem(), shouldn't I use getItemId() as per this explanation http://stackoverflow.com/a/6711888/1951418? – abdfahim Nov 09 '13 at 17:10
  • Glad I could help ;) In you case, you should use getItem, please refer to the following for more details on the different use cases for the 2 methods : http://stackoverflow.com/questions/6711592/what-is-the-intent-of-the-methods-getitem-and-getitemid-in-the-android-class-bas – 2Dee Nov 09 '13 at 17:28
-1

Use setTag(bookID) to set the tag to the view you are returning in getView in your MylibmanList and then retrieve that tag on onItemClick like view.getTag();

Apoorv
  • 13,470
  • 4
  • 27
  • 33
  • Could anyone explain why this answer was downvoted so i don't make the mistake again? – Apoorv Nov 09 '13 at 12:39
  • You are duplicating the data, the information is already held inside the Adapter, so there's no need to duplicate it. You can also refer to the following link for more on the drawbacks of setTag/getTag : http://stackoverflow.com/a/19364325/1178337 – 2Dee Nov 09 '13 at 17:34