我有一个ListActivity,我正在尝试使用自定义ArrayAdapter绑定到业务对象数组。
当我在模拟器或真实的Android设备上运行应用程序时,我在ArrayAdapter的“holder.txtTeam1.setText(info.Team1);”行中得到一个空引用。在进行一些调试之后,看起来调用膨胀布局实际上并没有创建子TextView,它只创建了outter TableLayout。
这完全基于我在互联网上找到的一个教程,而且我已经在桌面上敲了近一个星期。谁能告诉我我做错了什么?
活动如下:
public class BaseballPressActivity extends ListActivity {
private GameDataAdapter adapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
adapter = new GameDataAdapter(this, new GameData[]{new GameData("3:10pm","Team 1","Team 2")});
this.setListAdapter(adapter);
}
}
ArrayAdapter如下所示:
public class GameDataAdapter extends ArrayAdapter<GameData> {
Context context;
GameData data[] = null;
public GameDataAdapter(Context context, GameData[] data) {
super(context,R.layout.listitem_game, R.id.Name1, data);
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
GameViewHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(R.layout.test , parent, false);
holder = new GameViewHolder();
holder.txtTeam1 = (TextView)row.findViewById(R.id.Name1);
row.setTag(holder);
}
else
{
holder = (GameViewHolder)row.getTag();
}
GameData info = data[position];
holder.txtTeam1.setText(info.Team1);
return row;
}
static class GameViewHolder
{
TextView txtTeam1;
TextView txtTeam2;
TextView txtGameTime;
}
}
列表活动项目的布局如下所示:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:text="TextView" android:layout_weight="0" android:id="@+id/textView1" android:layout_height="wrap_content" android:layout_width="0dp"></TextView>
<ImageView android:id="@+id/imageView1" android:src="@drawable/icon" android:layout_height="wrap_content" android:layout_weight="0" android:layout_width="0dp"></ImageView>
<LinearLayout android:id="@+id/linearLayout1" android:orientation="vertical" android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="0dp">
<TextView android:text="TextView" android:id="@+id/Name1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:text="TextView" android:id="@+id/Name2" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
<ImageView android:id="@+id/imageView2" android:src="@drawable/icon" android:layout_height="wrap_content" android:layout_weight="0" android:layout_width="0dp"></ImageView>
</TableRow>
</TableLayout>
答案 0 :(得分:2)
row = inflater.inflate(R.layout.test , parent, false);
将false更改为true,我认为您必须将视图附加到它的父级。
如果不起作用,试试这个。
row = inflater.inflate(R.layout.test , null);
答案 1 :(得分:1)
放弃SimpleAdapter或SimpleCursorAdapter的ArrayLayout。这将允许您轻松地将数据绑定到简单到相当复杂的布局。