0

I have 2 variables inside Object object, when I print it using toString() it showed me {"name"="some_name","phone"="some_phone"} , is it possible to convert that object into string[0] = name and string[1]=phone?

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

                Object object = listView.getItemAtPosition(position);

            }
        });
10
  • getItemAtPosition returns an Object so what is the actual data type of your data Commented Mar 11, 2017 at 15:19
  • String[0] = (String) object.name;? Commented Mar 11, 2017 at 15:20
  • @LunarWatcher Object class doesn't have a name field so error Commented Mar 11, 2017 at 15:21
  • Given @FarellSujanto 's Object array, yes Commented Mar 11, 2017 at 15:24
  • the object is from list view's data , so how to take the data from list view to string? I need it for details activity and to search in the DB Commented Mar 11, 2017 at 15:29

1 Answer 1

1

That really depends on the class you're using to represent your items, if you'r storing them inside a list of HashMap as you indicated in the comment, you should cast your object to a HashMap, then you can easily access the keys and values of that hashmap:

HashMap<String, String> album  = (HashMap<String, String>) listView.getItemAtPosition(position);
String name = album.get("name");
String phone = album.get("phone");
// you can store them in an array
String[] attrs = {name, phone};

edit : you can also make your albums variable global, then you can replace the first line with simply:

HashMap<String, String> album = albums.get(position);
Sign up to request clarification or add additional context in comments.

1 Comment

thanks @PavneetSingh & nabil i've combine both of your codes and it works flawlessly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.