如何将android Object转换为String []?

时间:2017-03-11 15:14:57

标签: java android

我在Object对象中有2个变量,当我使用toString()打印它时,它显示我{“name”=“some_name”,“phone”=“some_phone”},是否可以将该对象转换为字符串[ 0] =姓名和字符串[1] =电话?

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

                Object object = listView.getItemAtPosition(position);

            }
        });

1 个答案:

答案 0 :(得分:1)

这实际上取决于您用来表示商品的课程,如果您将其存储在评论中指明的HashMap列表中,则应将对象投放到a HashMap,然后您可以轻松访问该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};

编辑:您还可以将albums变量设为全局,然后您可以简单地替换第一行:

HashMap<String, String> album = albums.get(position);