2016-06-24 85 views
0

由於我一年開發android應用程序,所以我無法解決這類問題。我在我們的朋友谷歌搜索了很多次,但0實際結果。這是一個非常精確的問題,我嘗試動態地顯示圖像轉換成列表視圖的物品,我的意思是:將圖像動態顯示到列表視圖項中

1-我接收int數組從我的數據庫(例如:5,6,7,7)

2-我想讓adpater顯示取決於此數字的不同圖像

例如,我收到:「result」= {「1」,「2」,「3」}我希望應用程序將圖像關聯到此數字(圖片來自可繪製文件夾)

if(int number = 1){imageview into item layo ut.setImageRessource(R.id.blabla) }其他...

我真的不知道該怎麼辦,我嘗試建立一個自定義適配器,但它不顯示列表視圖...

如果有人能告訴我什麼是這樣做的好方法,我將成爲最快樂的開發人員。

protected void showList() { 
 
    try { 
 
     JSONObject jsonObj = new JSONObject(myJSON2); 
 
     Poeple2 = jsonObj.getJSONArray(TAG_RESULTS); 
 

 
     for (int i = 0; i < Poeple2.length(); i++) { 
 
      JSONObject c = Poeple2.getJSONObject(i); 
 
      String username = c.getString(TAG_USERNAME); 
 
      int mood = c.getInt(TAG_MOOD); 
 

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

 
      persons.put(TAG_USERNAME, username); 
 
      persons.put(TAG_MOOD, String.valueOf(mood)); 
 
      personList2.add(persons); 
 
     } 
 

 
     // i used a simple adapter but i know it's a wrong way 
 
     adapter = new SimpleAdapter(getActivity(), personList2, R.layout.modeluser4, new String[]{TAG_USERNAME, TAG_MOOD}, new int[]{R.id.tvHypo2, R.id.tvId}); 
 
     list3.setAdapter(adapter); 
 

 
    } catch (JSONException e) { 
 
     e.printStackTrace(); 
 
    } 
 
}

回答

0

創建像

public void runTheSwitch(int num){ 
int number = num; 
switch(number){ 
case 1: 
    //add image to listview 
case 2: 
    //add image to listview 
case 3: 
    //add image to listview 

    ....and so on... 
    } 
} 

開關當您收到的數字從數據庫中的數組(可以稱之爲ArrayNum),運行通過這些NUM循環。

For(int num : ArrayNum){ 
    runTheSwitch(num); 

} 

把它放到方法中並在設置適配器之前運行該方法。
因此,基本上在這種方法中,您可以將項目添加到您的Arraylist中,如arraylist.add();
,然後在此之後定義您的自定義適配器的對象並將Arraylist傳遞到適配器中。
希望它有幫助

+0

我試着讓你知道謝謝你的建議:) –

+0

你把它放到適配器類中嗎?你可以更精確一點嗎? –

+0

謝謝你喔,我讓你知道的很多人:p –