【问题标题】:Display Multiple value in hashmap one by one一个一个地显示hashmap中的多个值
【发布时间】:2013-12-09 13:53:40
【问题描述】:

我想从第一个 hashmap 中获取选定的行并将其显示在另一个 hashmap 中。我成功地从第一个哈希图中获取了值并将其显示在第二个哈希图中。现在的问题是如何逐个显示从第一个哈希图中选择的值,因为我目前只能在第二个哈希图中显示一个值。我通过添加条目集或 notifyDataSetChanged 进行了大量研究,但仍然无法正常工作。如我错了请纠正我。请帮忙!谢谢!

这是我的代码。 LISTMENU 是第一个 hashmap,LISTODER 是第二个 hashmap。 LISTODER 可以从 LISTMENU 的 clicktener 中获取值。

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

            HashMap<String, String> map = LIST2.get(position);

            itemValue = map.get(FOODNAME2);
            itemID = map.get(FOODID2);

            Toast.makeText(getApplicationContext(),
                    "Food ID : " + itemID + "ListItem : " + itemValue , Toast.LENGTH_LONG)
                    .show();

            listOrder(itemValue, itemID);
         }
    });
}

private void listOrder(String itemValue, String itemID)
{   
    ArrayList<HashMap<String, String>> LIST3 = new ArrayList<HashMap<String, String>>();

    HashMap<String, String> map = new HashMap<String, String>();

    map.put(FOODID3, itemID);
    map.put(FOODNAME3, itemValue);  


    /*for (Map.Entry<String, String> entry : map.entrySet())
    {
        String key = entry.getKey();
        String value = entry.getValue();
    }*/

    LIST3.add(map);

    LISTORDER = (ListView) findViewById(R.id.listOrder);

    List3Adapter adapter = new List3Adapter(MainActivity.this, LIST3);
    LISTORDER.setAdapter(adapter);

    /*adapter.setNotifyOnChange(true);
    adapter.addAll(map);
    adapter.notifyDataSetChanged();*/

}

【问题讨论】:

    标签: java android listview hashmap


    【解决方案1】:

    我认为问题在于您每次都在创建一个新的 ArrayList 实例。将 ArrayList LIST3 声明为全局变量,然后在 onCreate 方法中对其进行初始化,在 listOrder 方法中对其进行访问,而无需像下面的代码 sn-p 那样创建 ArrayList 的新实例。希望这行得通。

    ArrayList<HashMap<String, String>> LIST3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);
    
        LIST3 = new ArrayList<HashMap<String, String>>();
    
    }
    
    LISTMENU.setOnItemClickListener(new OnItemClickListener() 
    {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
        {
    
            HashMap<String, String> map = LIST2.get(position);
    
            itemValue = map.get(FOODNAME2);
            itemID = map.get(FOODID2);
    
            Toast.makeText(getApplicationContext(),
                    "Food ID : " + itemID + "ListItem : " + itemValue , Toast.LENGTH_LONG)
                    .show();
    
            listOrder(itemValue, itemID);
         }
    });
    

    }

    private void listOrder(String itemValue, String itemID)
    

    {

    HashMap<String, String> map = new HashMap<String, String>();
    
    map.put(FOODID3, itemID);
    map.put(FOODNAME3, itemValue);  
    
    
    /*for (Map.Entry<String, String> entry : map.entrySet())
    {
        String key = entry.getKey();
        String value = entry.getValue();
    }*/
    
    LIST3.add(map);
    
    LISTORDER = (ListView) findViewById(R.id.listOrder);
    
    List3Adapter adapter = new List3Adapter(MainActivity.this, LIST3);
    LISTORDER.setAdapter(adapter);
    
    /*adapter.setNotifyOnChange(true);
    adapter.addAll(map);
    adapter.notifyDataSetChanged();*/
    

    }

    【讨论】:

    • 谢谢!它正在工作!我可以再问一个问题吗?是否可以从 LIST3 中获取所有值并将其放入另一个活动?
    • 查看answer。希望这能回答您的问题。
    • 你真是个好伙伴。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2015-06-28
    • 1970-01-01
    • 2018-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-15
    相关资源
    最近更新 更多