如何在ListView中获取对象并将其分配给变量?

时间:2015-04-13 10:34:33

标签: android listview

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

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
                AlertDialog.Builder builder = new AlertDialog.Builder(UserPage.this);
                builder.setTitle(rocketName);
                builder.setMessage("Are you sure to accept this order ? " + listView.getItemAtPosition(position) + "SMS will be sent to the customer.")
                        .setCancelable(false)
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
    //Assign value to variable here.
})

如何获取特定位置的详细信息并提取其中一个值并在单击列表视图时将其分配给变量?

logcat中的示例文本。列表视图中的一个项目:

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

我在public void onItemClick中尝试了以下方法:

TextView tv = (TextView)findViewById(android.R.id.text1);
                                    String str = String.valueOf(tv.getText());
                                    String orderID = str.split("Address: ")[1].split("Time Frame: ")[0];

它不起作用的原因是因为它总是显示第一个位置的文本,即使点击了其他位置。

我想在单击列表视图时提取列表视图中该特定位置的Address,并将其分配给String变量。

1 个答案:

答案 0 :(得分:0)

检查是否有效:

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

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

            }
          });