使用CustomViews从ListView中的所有行获取数据

时间:2013-04-08 07:01:44

标签: android listview custom-view

Android 2.3.3,MinSDK = 8

我有一个ListView,每行有CustomView(带有一个Image,两个textviews和一个复选框)。我有一个名为添加联系人的按钮。单击此按钮,我会检查是否选中了复选框。如果选中,我必须获取图像的值,以及两个textview并存储在数据库中。

我知道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("");
                }

            }

我得到的例外:::

// 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

//例外

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?
            }

        }

1 个答案:

答案 0 :(得分:1)

使用您想要存储的值的getter setter创建这样的内部类

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;
        }
    }

获取您希望在列表视图中显示的列表

  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);



        }       

在getView本身的onCompoundButton中设置你的复选框值,并设置为getter setter。这样你就可以轻松完成。

由于