【问题标题】:show HASHMAP values separately in onListItemClick在 onListItemClick 中分别显示 HASHMAP 值
【发布时间】:2015-05-12 05:44:04
【问题描述】:

我正在使用 HASHMAP 在列表的单个列表项中显示两个项目,如下面的代码所示。

public class MainActivity extends ListActivity {
HashMap<String, String> item;


 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ArrayList<Map<String, String>> list = buildData();
    String[] from = { "name", "purpose" };

    int[] to = { android.R.id.text1, android.R.id.text2 };

    SimpleAdapter adapter = new SimpleAdapter(this, list,
            android.R.layout.simple_list_item_2, from, to);
    setListAdapter(adapter);
    // setOnItemClickListener(selectLesson);
}

@Override
protected void onListItemClick(ListView l, View v, int    position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);
    // Map<String, String> nn = list.get(position);

    String item1 = (String)           getListAdapter().getItem(position).toString();
    // item.get(name);


    Toast.makeText(this, item1, Toast.LENGTH_LONG).show();
}

private ArrayList<Map<String, String>> buildData() {
    ArrayList<Map<String, String>> list = new ArrayList<Map<String,String>>();
    list.add(putData("Android", "Mobile"));
    list.add(putData("Windows7", "Windows7"));
    list.add(putData("iPhone", "iPhone"));
    return list;
}

private HashMap<String, String> putData(String name, String purpose) {
    item = new HashMap<String, String>();
    item.put("name", name);
    item.put("purpose", purpose);


    return item;
}

}

我想要的是,每当我单击一个 listitem 键值时,都应该在单独的 toasts 中显示。目前我在一个 toasts 中显示两者,不知道如何将它们分开。

【问题讨论】:

    标签: android listview arraylist hashmap


    【解决方案1】:

    当你点击一行时,onListItemClick 被触发。你可以用getItemAtPosition检索你点击的元素:

    HashMap<String, String> item = (HashMap<String, String>) l.getItemAtPosition(position);
    

    并使用 get 方法从项目中检索信息

     String string = item.get("the_key_you_want_to_retrieve");
    

    【讨论】:

      【解决方案2】:

      替换下面的代码

      String item1 = (String)getListAdapter().getItem(position).toString();
          Toast.makeText(this, item1, Toast.LENGTH_LONG).show();
      

      String item1 = getListAdapter().getItem(position).getString("name");
         String item2 = getListAdapter().getItem(position).getString("purpose");
          Toast.makeText(this, item1, Toast.LENGTH_LONG).show();
          Toast.makeText(this, item2, Toast.LENGTH_LONG).show();
      

      注意

      "name" --> 第一个键,根据您的选择

      “目的”->第二个键,根据您的选择

      【讨论】:

        【解决方案3】:

        你可以使用 split 函数来拆分“item1”

        String item1=(String)getListAdapter().getItem(position).toString();
        String[] parts = item1.split(" ");
        String part1 = parts[0]; // Android
        String part2 = parts[1]; // Mobile
         Toast.makeText(this, part1, Toast.LENGTH_LONG).show();
         Toast.makeText(this, part2, Toast.LENGTH_LONG).show();
        

        Split 函数用于根据给定条件拆分字符串。我在这里使用空格(“”)将 item1 拆分为两个字符串。

        【讨论】:

        • 它显示的输出类似于 first toast {PURPOSE=MOBILE, 2nd toast NAME=ANDROID} 我只想显示 MOBILE 和 ANDROID
        猜你喜欢
        • 2015-10-26
        • 1970-01-01
        • 1970-01-01
        • 2015-01-01
        • 2012-02-22
        • 2017-04-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多