3

Stackoverflow has really helped me get to this level. Thank you all.

Basically, I want to get an element (sub-part not the whole data) from the selected item in a ListView. I've read here that it could be done using Cursors but no success. This is the associated snippet:

listView.setAdapter(adapter);
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {

                    Cursor member = (Cursor)parent.getItemAtPosition(position);

                    Toast.makeText(getApplicationContext(),
                            "Click ListItem Number "+member.getString(2), Toast.LENGTH_LONG)
                            .show();
                }
            }); 

parent.getItemAtPosition(position) gives me the (Array, I suppose):

{name="xyz_name", uname="xyz_user", desig="xyz_desig"} correctly.

I want to, suppose, fetch unamefrom this, so, have used Cursor to fetch it but the above snippet is giving the error,

java.lang.ClassCastException: java.util.HashMap cannot be cast to android.database.Cursor

Adding detail: I'm getting the text associated with the listView item correctly. What I actually want a sub-part of the data retrieved. For example the data retrieved is obviously an array containing a name and his role. Now, I just want to fetch the 'role' element in the array. Since, the data is retrieved as an array, what I've searched on net is that I can use Cursors to retrieve different elements. Hope you got me now.

There's seem to be a silly syntax error by my side. If you have any alternatives, please tell.

Thanks :)

Baahubali
  • 163
  • 3
  • 14
  • 1
    Does this answer your question? [How to get object value from listview adapter position](https://stackoverflow.com/questions/14393334/how-to-get-object-value-from-listview-adapter-position) – user4157124 Mar 22 '22 at 17:58

2 Answers2

2

go with the simple way

ListView list = (ListView) findViewById(R.id.listview);
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
       @Override
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

          Object listItem = list.getItemAtPosition(position);

        Toast.makeText(getApplicationContext(),"Click ListItem Number"+listItem.getString(), Toast.LENGTH_LONG).show();
       } 
    });
Charuක
  • 12,953
  • 5
  • 50
  • 88
  • Thank you, but `Cannot resolve method getString()` The only method associated with `Object` Studio giving hint of is `getClass()` – Baahubali Dec 04 '16 at 11:47
  • Object is a generic type. what important is position. once you get it you know what is the position you clicked right? If you want to get the value of a text view in that list item no need to use Object, use what you want.. if its a TextView tv = list.getItemAtPosition(position); then you can get it to a string ... did you get that – Charuක Dec 04 '16 at 11:53
  • I'm getting the text associated with the listView item correctly. What I actually want a sub-part of the data retrieved. For example the data retrieved is obviously an array containing a name and his role. Now, I just want to fetch the 'role' element in the array. Since, the data is retrieved as an array, what I've searched on net is that I can use Cursors to retrieve different elements. Hope you got me now. – Baahubali Dec 04 '16 at 12:54
0

Understanding your case, getItemAtPosition(position) is returning you an Object.

The answer posted by @Charuka Silva might not be working for you as you may have used hashmap object something like HashMap<String,String> obj = new HashMap<String,String>();

So, the solution is to use HashMap again for getting the returned object and using get("key") function to retrieve the value associated with it. I think this is what you want:

listView.setAdapter(adapter);
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
//Use HashMap here
                    HashMap<String, Object> member = (HashMap<String, Object>) parent.getItemAtPosition(position);


//Use obj.get("key") here to retrieve the value associated with the "key"
              Toast.makeText(getApplicationContext(),"member.get("uname"), Toast.LENGTH_LONG)
                            .show();
                }
            });

I had a similar problem in my Minor Project and an answer by @user1068963 helped me. Adding Link.

Hope this helps.

Community
  • 1
  • 1
Baahubali
  • 163
  • 3
  • 14