gpt4 book ai didi

android - 具有不同布局的 ListView 行

转载 作者:搜寻专家 更新时间:2023-11-01 08:09:01 25 4
gpt4 key购买 nike

我想要实现的是每一行都有不同的布局。我的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)。

有什么建议吗?在过去的一天里,我一直在为此苦苦挣扎。

最佳答案

NullPointerException 的发生是因为您没有实现 getItemViewType。现在你无论如何都会返回第一种类型的行布局,这是一个没有 *txtView_test* TextView 的布局。你的代码应该是这样的:

// some fields
public static final int FIRST_TYPE = 0;
public static final int SECOND_TYPE = 1;

@Override
public int getItemViewType(int position) {
MyContents item = getItem(position);
if (item.testBoolean()) {
return FIRST_TYPE;
} else {
return SECOND_TYPE;
}
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
final MyContents entry = content.get(position);
View row = convertView;
LayoutInflater inflater = null;
int type = getItemViewType(position);
boolean _haschild_ = entry.testBoolean();
if (row == null) {
if (type == FIRST_TYPE) {
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 (type == FIRST_TYPE) {
TextView txtView = (TextView) row
.findViewById(R.id.txtView);
//.....
} else {
((TextView) row.findViewById(R.id.txtView_test)) //**LOE**
.setText("test: " + position); //**LOE**
}

关于android - 具有不同布局的 ListView 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11645160/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com