我的应用程序中有一个可扩展的列表视图。当所有的孩子都是TextView时,一切正常。 但现在我想尝试不同的儿童类型。这意味着,例如对于第一组我想要TextView子项,对于第二组我想要EditText子项,而对于所有其他组我想要有radioButton子项。 我试着编码但是我得到了一个BUG。对于同一组,我得到不同类型的视图(EditTexts,RadioButtons和TextViews)。我认为这是由于 convetView ,因为在第一次点击全部OK然后问题就开始了。
这是我的getChildView方法:
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String laptop = (String) getChild(groupPosition, childPosition);
LayoutInflater inflater = context.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.child_item, null);
}
TextView item=null;
EditText itemEdit=null;
RadioButton itemRadio=null;
item = (TextView) convertView.findViewById(R.id.laptop);
itemEdit = (EditText) convertView.findViewById(R.id.laptopEdit);
itemRadio = (RadioButton) convertView.findViewById(R.id.laptopRadio);
if(groupPosition==0){
itemEdit.setVisibility(View.GONE);
itemRadio.setVisibility(View.GONE);
}else if(groupPosition==1){
item.setVisibility(View.GONE);
itemRadio.setVisibility(View.GONE);
}else{
itemEdit.setVisibility(View.GONE);
item.setVisibility(View.GONE);
}
ImageView delete = (ImageView) convertView.findViewById(R.id.delete);
delete.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Do you want to remove?");
builder.setCancelable(false);
builder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
List<String> child =laptopCollections.get(laptops.get(groupPosition));
child.remove(childPosition);
notifyDataSetChanged();
}
});
builder.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
});
if(groupPosition==0)
item.setText(laptop);
else if(groupPosition==1)
itemEdit.setText(laptop);
else
itemRadio.setText(laptop);
return convertView;
}
这是我的&#34; child_item.xml&#34;
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:background="#196387">
<TextView
android:id="@+id/laptop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:paddingLeft="25dp"
android:textColor="#dddddd"/>
<EditText
android:id="@+id/laptopEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:paddingLeft="25dp"
android:textColor="#dddddd"/>
<RadioButton
android:id="@+id/laptopRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:paddingLeft="25dp"
android:textColor="#dddddd"/>
<ImageView
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="@drawable/ic_delete"
android:contentDescription="@string/app_name"/>
</RelativeLayout>