Android 2.3.3, MinSDK = 8
I have a ListView, with CustomView (with an Image, two textviews and 1 checkbox) for each row. I have a button called Add Contacts. On click of this button, I have the check whether the checkbox is checked or not. If Checked, I have to get the values of the image, and two textviews and store in database.
I know how ListView's CustomView works. The views get created as and when they are displayed. So, how do I get all the rows that are checked?
Here is the XML layout :::
<?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>
Here is the code that i am trying - though I know, it doesn't work :::
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("");
}
}
And the Exception I get :::
// OutPut
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
// Exception
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)
The exception is coming because, the views are not yet created. But, then, how do i get the checked values in the entire list.
EDIT :::
If i choose to implement onCheckedChange() for the checkbox, How do i get the ListView Item, of the current checked checkbox, inside the onCheckedChange() method?
@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?
}
}
Make an inner class like this with the getter setters for your values which you want to store
class CommonCategory {
private String nameofFriend;
private boolean isChecked;
public void setName(String nameofFriend) {
this.nameofFriend = nameofFriend;
}
public String getName() {
return nameofFriend;
}
public void setChecked(boolean isChecked) {
this.isChecked = isChecked;
}
public boolean isChecked() {
return isChecked;
}
}
Get your list like this which you want to show in the list view
final ArrayList<CommonCategory> commonFriends = new ArrayList<CommonCategory>();
for (FriendsDataClass friendsDataClass : XMPPConn.mfriendList) {
String usernameEmail = friendsDataClass.friendName;
String username = usernameEmail.substring(0,
usernameEmail.indexOf("@"));
CommonCategory commoncontacts = new CommonCategory();
commoncontacts.setUsernameEmail(usernameEmail);
commoncontacts.setName(username);
commoncontacts.setChecked(false);
commonFriends.add(commoncontacts);
}
And do set your checkbox value in onCompoundButton in the getView itself, and set is to the getter setter. This way you can do it very easily.
Thanks