2014-04-15 186 views
0

正如标题所述,我遇到了一些麻烦让我的自定义列表视图适配器正常工作。该应用程序不会崩溃,但它也没有显示在列表中。我用我已经设置好的一个简单列表测试了我的数据,而且工作得很好。使用自定义列表视图适配器

History.java

public class History { 
    public String score; 
    public String gametype; 
    public int icon; 

    public History() { 
     super(); 
    } 

    public History(String score, String gametype, int icon) { 
     super(); 
     this.score = score; 
     this.gametype = gametype; 
     this.icon = icon; 
    } 
} 

HistoryAdapter.java

public class HistoryAdapter extends ArrayAdapter<History> { 

    Context context; 
    int layoutResId; 
    History data[] = null; 

    public HistoryAdapter(Context context, int layoutResId, History[] data) { 
     super(context, layoutResId, data); 
     this.layoutResId = layoutResId; 
     this.context = context; 
     this.data = data; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     HistoryHolder holder = null; 

     if(convertView == null) 
     { 
      LayoutInflater inflater = ((Activity)context).getLayoutInflater(); 
      convertView = inflater.inflate(layoutResId, parent, false); 

      holder = new HistoryHolder(); 
      holder.imageIcon = (ImageView)convertView.findViewById(R.id.icon); 
      holder.textTitle = (TextView)convertView.findViewById(R.id.gameType); 
      holder.textScore = (TextView)convertView.findViewById(R.id.score); 

      convertView.setTag(holder); 
     } 
     else 
     { 
      holder = (HistoryHolder)convertView.getTag(); 
     } 

     History history = data[position]; 
     holder.textScore.setText(history.score); 
     holder.textTitle.setText(history.gametype); 
     holder.imageIcon.setImageResource(history.icon); 


     return convertView; 
    } 

    static class HistoryHolder 
    { 
     ImageView imageIcon; 
     TextView textTitle; 
     TextView textScore; 
    } 
} 

实施

for(int i = 0; i < games.length(); i++) { 
        JSONObject c = games.getJSONObject(i); 
        JSONObject gameStats = games.getJSONObject(i).getJSONObject(TAG_STATS); 
        type[i] = c.getString(TAG_TYPE); 
        champId[i] = c.getString("championId"); 
        cs[i] = gameStats.getString("minionsKilled"); 
        kills[i] = gameStats.getString("championsKilled"); 
        deaths[i] = gameStats.getString("numDeaths"); 
        assists[i] = gameStats.getString("assists"); 
        win[i] = gameStats.getString("win"); 

        if(win[i].equals("true")) 
         win[i] = "Victory"; 
        else 
         win[i] = "Defeat"; 

        if(type[i].equals("RANKED_SOLO_5x5")) 
         type[i] = "Ranked (Solo)"; 

        if(type[i].equals("CAP_5x5")) 
         type[i] = "TeamBuilder"; 

        if(type[i].equals("NORMAL")) 
         type[i] = "Unranked"; 

        score[i] = kills[i] +"/" + deaths[i] + "/" + assists[i]; 

        historyData[i] = new History(score[i], champId[i], R.drawable.ic_launcher); // Placeholder image 

       } 

       adapter = new HistoryAdapter(MatchHistoryActivity.this, 
         R.layout.list_adapter, 
         historyData); 

       list.setAdapter(adapter); 

listview.xml

<?xml version="1.0" encoding="utf-8"?> 

<ListView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/list" 
    android:background="#111111"> 
    </ListView> 

list_item.xml

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:background="#111111" 
    android:padding="6dip" > 

    <ImageView 
     android:id="@+id/icon" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginRight="6dip" 
     android:contentDescription="TODO" 
     android:src="@drawable/ic_launcher" /> 

    <TextView 
     android:id="@+id/score" 
     android:textColor="#C49246" 
     android:layout_width="fill_parent" 
     android:layout_height="26dip" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:layout_marginLeft="5dp" 
     android:layout_toRightOf="@id/icon" 
     android:ellipsize="marquee" 
     android:singleLine="true" 
     android:text="0/0/0 KDA" 
     android:textSize="12sp" /> 

    <TextView 
     android:id="@+id/gameType" 
     android:textColor="#C49246" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_above="@id/score" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:layout_alignWithParentIfMissing="true" 
     android:layout_marginLeft="5dp" 
     android:layout_toRightOf="@id/icon" 
     android:gravity="center_vertical" 
     android:textSize="16sp" /> 

</RelativeLayout> 

回答

1

您正在创建一个History对象与此行:

new History(score[i], champId[i], R.drawable.ic_launcher); // Placeholder image 

这只是调用构造函数,并为垃圾收集器再次带走的对象。

您实际上并未将此History对象添加到historyData阵列。


循环之前,您应该声明 historyData数组:

History[] historyData = new History[games.length(); 

而且在循环,你应该创建的对象分配给数组

historyData[i] = new History(score[i], champId[i], R.drawable.ic_launcher); // Placeholder image 
+0

哎呀。应该看到了。即使如此,屏幕上也没有显示任何内容,只是一个空白的白色屏幕。 – Nate

+0

任何想法,为什么这可能是? – Nate

+0

@Nate - 你必须用调试器来完成它。如果您在HistoryAdapter.getView()上放置断点,它会被调用吗? –

相关问题