Riven
Riven

Reputation: 796

How to convert android Object to String[]?

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

            }
        });

Upvotes: 0

Views: 434

Answers (1)

nabil london
nabil london

Reputation: 521

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

Upvotes: 1

Related Questions