Android ListView使用自定义适配器不显示任何内容
正如标题所述,我在让自定义listview正常工作时遇到了一些问题。该应用程序在列表中不显示任何内容,只显示一个空白的白色屏幕。我用一个已经设置好的简单列表测试了数据,效果很好。我希望有人能看到我没有看到的东西。谢谢 History.javaAndroid ListView使用自定义适配器不显示任何内容,android,android-listview,Android,Android Listview,正如标题所述,我在让自定义listview正常工作时遇到了一些问题。该应用程序在列表中不显示任何内容,只显示一个空白的白色屏幕。我用一个已经设置好的简单列表测试了数据,效果很好。我希望有人能看到我没有看到的东西。谢谢 History.java public class History { public String score; public String gametype; public int icon; public History() {
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;
}
}
公共类HistoryAdapter扩展了ArrayAdapter{
语境;
int layoutResId;
历史数据[]=null;
公共HistoryAdapter(上下文上下文、int-layoutResId、历史[]数据){
super(上下文、layoutResId、数据);
this.layoutResId=layoutResId;
this.context=上下文;
这个数据=数据;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
HistoryHolder=null;
if(convertView==null)
{
LayoutInflater充气器=((活动)上下文)。getLayoutInflater();
convertView=充气机。充气(layoutResId,父项,false);
holder=新历史holder();
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=(HistoryHolder)convertView.getTag();
}
历史=数据[位置];
holder.textScore.setText(history.score);
holder.textTitle.setText(history.gametype);
holder.imageIcon.setImageResource(history.icon);
返回视图;
}
静态类历史持有者
{
图像视图图像图标;
文本视图文本标题;
TextView textScore;
}
}
实施
History[] historyData = new History[games.length()];
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
}
if(historyData == null) {
historyData[0] = new History("No game found", "N/A", R.drawable.ic_launcher);
Log.i("Data", "" + historyData);
}
adapter = new HistoryAdapter(MatchHistoryActivity.this,
R.layout.list_adapter,
historyData);
list.setAdapter(adapter);
History[]historyData=新历史[games.length()];
对于(int i=0;i
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>
我猜您使用了错误的XML 改变
adapter = new HistoryAdapter(MatchHistoryActivity.this,
R.layout.list_adapter,historyData);
到
并确保您的
historyData
不为空您包含的代码似乎没有问题,因此它唯一可能失败的地方是在创建historyData
数组的循环中
由于某些原因,您可能无法正确处理它,例如,有时未定义某些JSON
属性
您可以使用try
块捕获JSONException
以查看错误,还可以添加几个Log.d()
句子以了解罪犯在哪里
例如:
try {
cs[i] = gameStats.getString("minionsKilled");
}
catch (JSONException) { e.printStackTrace(); }
在我看来,整个代码都还可以,只有一件事:您确定在声明
ArrayAdapter
时historyData
不是空的吗?@nKn我添加了一个代码部分来检查historyData是否为空,我相信我正确地实现了这一点。如果你能看一下,那就太好了。你还应该在循环中放一些Log.d()
句子来检查它是否在做它应该做的事情,还应该包括初始化historyData
@nKn的部分。我在初始化它的地方加了一些,但我遇到了一点问题。在循环中使用Log.d()语句,我发现循环正在停止。原因是,当游戏的死亡人数为0时,返回的JSON根本不包括该字段。因此,JSON中没有gameStats.getString(“numdeath”)条目。在某些情况下,我如何处理不在那里的情况?因此,基本上用try包围.getString()
语句,如果它引发JSONException
,您就知道它不在那里。可能你的第一期文章就是从这里开始的。为了便于理解,我更改了标题中的文件名。我的list_adapter.xml文件实际上是典型的list_item.xml文件。同样,我的list_adapter.xml的内容是上面发布的list_item.xml。您是否检查了您的历史数据是否为null?我添加了一段代码来检查它是否为null,并且它没有打印一个表明它为null的日志。我已经将添加的代码编辑到上面的主帖子中。
try {
cs[i] = gameStats.getString("minionsKilled");
}
catch (JSONException) { e.printStackTrace(); }