2013-12-09 40 views
1

我想從第一個哈希映射中獲取選定的行並將其顯示在另一個哈希映射中。我成功地從第一個hashmap獲取值並將其顯示在第二個hashmap中。現在問題是如何顯示從第一個hashmap中選擇的值,因爲我目前只能在第二個hashmap中顯示一個值。我通過添加入口集或notifyDataSetChanged做了大量的研究,但仍然無法正常工作。如我錯了請糾正我。請幫忙!謝謝!在哈希映射中逐個顯示多個值

這是我的代碼。 LISTMENU是第一個hashmap,而LISTORDER是第二個hashmap。 LISTORDER可以從LISTMENU的點擊器獲得值。

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();*/ 

} 

回答

1

我認爲這個問題是因爲你每次都在創建一個新的ArrayList實例。將ArrayList LIST3聲明爲全局變量,然後在onCreate方法中初始化它,在listOrder方法中訪問它,而不像下面的代碼片段那樣創建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();*/ 

}

+0

謝謝!它正在工作!我能再有一個問題嗎?是否有可能從LIST3和putExtra獲得所有的價值到另一個活動? – user3040029

+1

選中此問題[答案](http://stackoverflow.com/questions/17718442/pass-the-arraylist-from-one-activity-to-another)。希望這會回答你的查詢。 –

+0

你真是個好人。謝謝! – user3040029