我正在开发这个应用程序,我在列表视图中有一个TextView和一个Checkbox。 ListView行的XMLS如下:
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="8"
android:orientation="vertical"
android:id="@+id/linearLayout">
<TextView
android:text="Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/deliverNumberTextView"
android:textSize="20dp"
android:textColor="@android:color/black"
android:padding="5dp" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:id="@+id/listviewCheckbox"/>
</LinearLayout>
我试图在ListView适配器中以编程方式选择TextView和Checkbox的值。以下是用于GetView()覆盖的代码:
public override View GetView (int position, View convertView, ViewGroup parent)
{
View row = convertView;
if (row == null)
{
row = LayoutInflater.From (_context).Inflate (Resource.Layout.ListviewRow, null, false);
}
TextView deliverNumberTextView = (TextView) row.FindViewById (Resource.Id.deliverNumberTextView);
CheckBox listviewCheckbox = (CheckBox)row.FindViewById(Resource.Id.listviewCheckbox);
deliverNumberTextView.Text = _deliveries[position].DeliveryNumber;
listviewCheckbox.Visibility = ViewStates.Visible;
return row;
}
问题是在执行此代码后,我获得了TextView的值,但是我无法更改Checkbox的可见性,因为“listviewCheckbox”为null。
感谢任何帮助。谢谢。
答案 0 :(得分:1)
尝试这种方式::
public class ListAdapter: BaseAdapter
{
// Make this variable global
TextView deliverNumberTextView ;
CheckBox listviewCheckbox
public override View GetView (int position, View convertView, ViewGroup parent)
{
View row = convertView;
if (row == null)
{
row = LayoutInflater.From (_context).Inflate (Resource.Layout.ListviewRow, null, false);
}
deliverNumberTextView = (TextView) row.FindViewById (Resource.Id.deliverNumberTextView);
listviewCheckbox = (CheckBox)row.FindViewById(Resource.Id.listviewCheckbox);
deliverNumberTextView.Text = _deliveries[position].DeliveryNumber;
listviewCheckbox.Visibility = ViewStates.Visible;
return row;
}
}