使用this,我尝试创建一个自定义ListAdapter,将所有记录添加为CheckBoxes。即使我在调试过程中看到适配器包含所有4条预期记录,并且重写int Count方法返回4,并且重写View GetView迭代4次,位置变量始终为0,只有1个包含第一条记录的CheckBox实际上显示。
ListView行布局xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<CheckBox
android:id="@+id/checkItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
CallManager.axml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="@+id/equipLayout"
android:tag="equip"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_height="fill_parent"
android:layout_below="@id/place4"
android:layout_margin="8dip"
android:visibility="gone">
<ListView
android:id="@+id/equipLV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
</RelativeLayout>
</ScrollView>
//还有其他几个嵌套的LinearLayouts,这就是为什么我有ScrollView和RelativeLayouts的原因。不相关,因此出于清洁考虑。
呼叫管理
public class CallManagement : Activity
{
CustomListAdapter listAdapter;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.CallManager);
ListView equipLV = (ListView)FindViewById(Resource.Id.equipLV);
listAdapter = new CustomListAdapter(this, callNumber.Substring(0,10));
equipLV.Adapter = listAdapter;
}
自定义ListAdapter类
public class CustomListAdapter : BaseAdapter
{
Activity context;
External inst;
public List<equip> equip;
public CustomListAdapter(Activity context, string callNumber)
:base()
{
inst = new External();
this.context = context;
this.equip = new List<equip>();
this.equip = inst.popEquipLV().ToList();
}
public override int Count
{
get { return equip.Count; }
}
public override Java.Lang.Object GetItem(int position)
{
return position;
}
public override long GetItemId(int position)
{
return position;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var item = equip[position];
var view = (convertView ??
context.LayoutInflater.Inflate(Resource.Layout.equipLVitem, parent, false)) as LinearLayout;
CheckBox checkItem = (CheckBox)view.FindViewById(Resource.Id.checkItem);
checkItem.Tag = GetItemAtPosition(position).unit.ToString();
checkItem.Text = GetItemAtPosition(position).serial.ToString();
return view;
}
public equip GetItemAtPosition(int position)
{
return equip[position];
}
}
最佳答案
您永远不想将ListView
放在ScrollView
内。那可能是问题所在。