Baahubali
Baahubali

Reputation: 163

Get element from the selected item in the listView

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 :)

Upvotes: 3

Views: 5017

Answers (2)

Baahubali
Baahubali

Reputation: 163

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.

Upvotes: 0

Charuka Silva
Charuka Silva

Reputation: 13153

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();
       } 
    });

Upvotes: 2

Related Questions