使用this我試圖創建一個自定義ListAdapter,只需將所有記錄添加爲CheckBoxes即可。儘管我可以在調試過程中看到適配器包含所有4條預期記錄,並且override int Count方法返回4,並且覆蓋View GetView迭代4次,但位置變量always = 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這就是爲什麼我有滾動型和RelativeLayouts。不相關,因此不需要清潔。
CallManagement.cs
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];
}
}
刪除ScrollView似乎已經奏效,但如果我需要ScrollView呢?沒有辦法讓這兩件事情一起工作嗎? – jmease 2012-02-20 14:06:07
沒有。如果你有一個滾動容器在另一個滾動容器內並且它們都朝相同方向滾動,它應該如何知道如何滾動?你需要兩個滾動容器來做什麼? – 2012-02-20 19:12:39