简体   繁体   English

使用 SimpleAdapter 从多列 ListView 中获取单个字段

[英]Get a single field from multi-column ListView using SimpleAdapter

I have a ListActivity in which i have used a SimpleAdapter to create a list which has 2 fields.我有一个 ListActivity,我在其中使用 SimpleAdapter 创建了一个包含 2 个字段的列表。 The pair of values are stored using a Map, and the list is an ArrayList. I have an onItemClickListener for this.这对值使用 Map 存储,列表是 ArrayList。为此我有一个 onItemClickListener。 On selecting a list entry, i get the pair ie the 2 values.在选择列表条目时,我得到了一对,即 2 个值。 I need to get only 1 of those values.我只需要获得这些值中的一个。

For example, if the list item is "John", "123".例如,如果列表项是“John”,则为“123”。 I want to store "123" in a string after selecting the entry from the list Any help?从列表中选择条目后,我想将“123”存储在字符串中有帮助吗?

Here's the code snippet:这是代码片段:

        ListView lv = getListView();
        ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();

        list.add(putData(scanned_name, scanned_addr));

        String[] from = { "name", "address" };
        int[] to = { android.R.id.text1, android.R.id.text2 };

        SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(), list,
                android.R.layout.simple_list_item_2, from, to);
        setListAdapter(adapter);

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


                Toast.makeText(getApplicationContext(),
                        parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
            }

        });

You have to catch the content of android.R.id.text1 or android.R.id.text2 .您必须捕获android.R.id.text1android.R.id.text2的内容。 So, in your onItemClick(...) you have to do:因此,在您的onItemClick(...)中,您必须执行以下操作:

TextView v = (TextView)findViewById(R.id.text1); TextView v = (TextView)findViewById(R.id.text1);
TextView v1 = (TextView)findViewById(R.id.text2); TextView v1 = (TextView)findViewById(R.id.text2);
String content1=v.getText().toString(); String content1=v.getText().toString();
String content2=v2.getText().toString(); String content2=v2.getText().toString();

and you can manage them as you want, even within a Toast:您可以根据需要管理它们,甚至在 Toast 中也是如此:

Toast.makeText(getApplicationContext(),v.getText().toString(),Toast.LENGTH_LONG).show(); Toast.makeText(getApplicationContext(),v.getText().toString(),Toast.LENGTH_LONG).show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM