如何在ListView中获取对象?

时间:2015-04-13 05:16:24

标签: android

我已经尝试过以下代码,但它无效。

SimpleAdapter simpleAdapter = new SimpleAdapter(this, employeeList,
                android.R.layout.simple_list_item_1,
                new String[] { "Address" }, new int[] { android.R.id.text1 });
        listView.setAdapter(simpleAdapter);

TextView tv = (TextView)findViewById(android.R.id.text1);
address = tv.getText().toString();
Log.d("Address: ", address);

在我的logcat中:

Address:﹕ John Smith
    Contact Number: 9999999
    Address: 41 Texas 55
    Time Frame: 5:00pm - 6:00pm
    ID: 5787
    Submitted at: 2015-03-15 11:18:23

我想要的只是获取地址:41 Texas 55并将其存储在String变量中。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:2)

试一试:

String str = String.valueOf(tv.getText());  
String address = str.split("Address: ")[1].split("Time Frame: ")[0];

<强>更新

说明:

首先:我在textView中获取字符串。所以str会满足:

String str = "John Smith
    Contact Number: 9999999
    Address: 41 Texas 55
    Time Frame: 5:00pm - 6:00pm
    ID: 5787
    Submitted at: 2015-03-15 11:18:23";

其次,我使用split将str分成2个和平。

str.split("Address: ")

所以:

str.split("Address: ")[0]

等于:

"John Smith \n Contact Number: 9999999"

str.split("Address: ")[1]将等于

"41 Texas 55
Time Frame: 5:00pm - 6:00pm
ID: 5787
Submitted at: 2015-03-15 11:18:23"

最后,我再次分裂str.split("Address: ")[1].split("Time Frame: ")

所以:address = str.split("Address: ")[1].split("Time Frame: ")[0]