我想要实现的是为每一行设置不同的布局。我的代码ArrayAdapter
如下:
@Override
public int getCount() {
return contentList.size();
}
@Override
public MyContents getItem(int position) {
return content.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return 2;
}
@Override
public int getItemViewType(int position) {
// TODO Auto-generated method stub
return super.getItemViewType(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final MyContents entry = content.get(position);
View row = convertView;
LayoutInflater inflater = null;
boolean _haschild_ = entry.testBoolean();
if (row == null) {
if (entry.testBoolean()) {
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater
.inflate(R.layout.contents_layout, null);
} else {
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.test_layout, null);
}
}
if (entry.testBoolean()) {
TextView txtView = (TextView) row
.findViewById(R.id.txtView);
//.....
} else {
((TextView) row.findViewById(R.id.txtView_test)) //**LOE**
.setText("test: " + position); //**LOE**
}
return row;
}
*test_layout.xml* 的布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_test"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:background="@color/grey_five"
android:orientation="vertical"
android:padding="@dimen/padding_small" >
<TextView
android:id="@+id/txtView_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="@string/lorem__ipsum"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/grey" />
</RelativeLayout>
但是每当我执行它时,它都会抛出一个NullPointerException
(检查带有注释的行 // LOE)。
有什么建议该怎么做吗?过去一天我一直在努力解决这个问题。