2016-06-24 86 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 –