安卓 2.3.3,MinSDK = 8
我有一个 ListView,每行都有 CustomView(带有一个图像、两个文本视图和 1 个复选框)。我有一个名为“添加联系人”的按钮。单击此按钮时,我检查复选框是否已选中。如果选中,我必须获取图像的值和两个文本视图并存储在数据库中。
我知道 ListView 的 CustomView 是如何工作的。视图在显示时创建。那么,如何获取所有检查的行?
这是 XML 布局 :::
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_margin="2dp"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imgView_addcontact"
android:layout_width="0dip"
android:layout_height="100dp"
android:layout_weight="0.20"
android:padding="5dp"
android:src="@drawable/addcontactsmall2" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:layout_weight="0.60"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:id="@+id/txtView_addcontact_contactname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Contact Name"
android:textSize="20sp" />
<TextView
android:id="@+id/txtview_addcontact_phonenumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phone Number"
android:textSize="16sp" />
</LinearLayout>
<CheckBox
android:id="@+id/chbk_addcontact"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight="0.10"
android:layout_gravity="center"
android:padding="5dp" />
</LinearLayout>
这是我正在尝试的代码 - 虽然我知道,但它不起作用:::
for(int i = 0; i< lv_addcontacts.getCount(); i++)
{
View contactView = lv_addcontacts.getChildAt(i);
CheckBox contactView_chbk = (CheckBox) contactView.findViewById(R.id.chbk_addcontact);
TextView contactView_txt_contactname = (TextView) contactView.findViewById(R.id.txtView_addcontact_contactname);
TextView contactView_txt_phonenumber = (TextView) contactView.findViewById(R.id.txtview_addcontact_phonenumber);
if(contactView_chbk.isChecked())
{
System.out.println("Contact Name = " + contactView_txt_contactname.getText().toString());
System.out.println("Phone Number = " + contactView_txt_phonenumber.getText().toString());
System.out.println("CheckBox Status = Checked");
System.out.println("");
}
else
{
System.out.println("Contact Name = " + contactView_txt_contactname.getText().toString());
System.out.println("Phone Number = " + contactView_txt_phonenumber.getText().toString());
System.out.println("CheckBox Status = Not Checked");
System.out.println("");
}
}
我得到的例外:::
// 输出
04-08 12:28:47.539: I/System.out(3244): Contact Name = Self Help
04-08 12:28:47.539: I/System.out(3244): Phone Number = *111#
04-08 12:28:47.539: I/System.out(3244): CheckBox Status = Not Checked
04-08 12:28:47.539: I/System.out(3244): Contact Name = Bonus Cards
04-08 12:28:47.539: I/System.out(3244): Phone Number = *444#
04-08 12:28:47.539: I/System.out(3244): CheckBox Status = Checked
04-08 12:28:47.539: I/System.out(3244): Contact Name = Annaya
04-08 12:28:47.539: I/System.out(3244): Phone Number = +1xxxx
04-08 12:28:47.539: I/System.out(3244): CheckBox Status = Not Checked
// 例外
04-08 12:28:47.539: W/dalvikvm(3244): threadid=1: thread exiting with uncaught exception (group=0x401be560)
04-08 12:28:47.539: E/AndroidRuntime(3244): FATAL EXCEPTION: main
04-08 12:28:47.539: E/AndroidRuntime(3244): java.lang.NullPointerException
04-08 12:28:47.539: E/AndroidRuntime(3244): at com.xxx.xx.Class_Add_Contact.onClick(Class_Add_Contact.java:277)
04-08 12:28:47.539: E/AndroidRuntime(3244): at android.view.View.performClick(View.java:2485)
04-08 12:28:47.539: E/AndroidRuntime(3244): at android.view.View$PerformClick.run(View.java:9080)
04-08 12:28:47.539: E/AndroidRuntime(3244): at android.os.Handler.handleCallback(Handler.java:587)
04-08 12:28:47.539: E/AndroidRuntime(3244): at android.os.Handler.dispatchMessage(Handler.java:92)
04-08 12:28:47.539: E/AndroidRuntime(3244): at android.os.Looper.loop(Looper.java:130)
04-08 12:28:47.539: E/AndroidRuntime(3244): at android.app.ActivityThread.main(ActivityThread.java:3687)
04-08 12:28:47.539: E/AndroidRuntime(3244): at java.lang.reflect.Method.invokeNative(Native Method)
04-08 12:28:47.539: E/AndroidRuntime(3244): at java.lang.reflect.Method.invoke(Method.java:507)
04-08 12:28:47.539: E/AndroidRuntime(3244): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
04-08 12:28:47.539: E/AndroidRuntime(3244): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
04-08 12:28:47.539: E/AndroidRuntime(3244): at dalvik.system.NativeStart.main(Native Method)
异常即将到来,因为尚未创建视图。但是,那么,我如何获得整个列表中的检查值。
编辑 :::
如果我选择为复选框实现 onCheckedChange(),如何在 onCheckedChange() 方法中获取当前选中复选框的 ListView 项?
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
// How do I get the View(CustomView of ListView) to access the textViews and ImageView?
}
}