如何在列表视图中仅检查联系人详细信息并保留未经检查的联系人?

时间:2013-02-05 19:30:55

标签: android listview checkbox contacts

我将联系人加载到列表视图中,现在我想要返回仅检查过的联系人的详细信息,如姓名和电话号码。我如何获得详细信息?列表视图中的详细信息存储在哪里,如位置,ID等。给出了代码

String[] from = { "Name", "Phone","chkbox" };
        int[] to = { R.id.txtContactName, R.id.txtContactNumber,R.id.checkBox1 };
        ArrayList<Map<String,String>> list=buildData();
        SimpleAdapter adapter = new SimpleAdapter(this, list,
                R.layout.main, from, to);
        setListAdapter(adapter);

private ArrayList<Map<String,String>> buildData() {
        ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
        list.clear();
        Cursor people = getContentResolver().query(
                ContactsContract.Contacts.CONTENT_URI, null, null, null,
                "UPPER(" + ContactsContract.Contacts.DISPLAY_NAME + ") ASC");
        while (people.moveToNext()) {
            String contactName = people.getString(people
                    .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            String contactId = people.getString(people
                    .getColumnIndex(ContactsContract.Contacts._ID));
            String hasPhone = people
                    .getString(people
                            .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
            if ((Integer.parseInt(hasPhone) > 0)) {
                Cursor phones = getContentResolver().query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                + " = " + contactId,
                        null,
                        "UPPER(" + ContactsContract.Contacts.DISPLAY_NAME
                                + ") ASC");
                while (phones.moveToNext()) {
                    String phoneNumber = phones
                            .getString(phones
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    Map<String, String> NamePhoneType = new HashMap<String, String>();
                    NamePhoneType.put("Name", contactName);
                    NamePhoneType.put("Phone", phoneNumber);
                    list.add(NamePhoneType);
                     aa=contactName;
                     bb=phoneNumber;
                }
                phones.close();
            }
        }
        people.close();
        return list;
    }


@Override
    protected void onListItemClick(ListView l, View v, int position, long id) {

        super.onListItemClick(l, v, position, id);
        CheckBox checkbox = (CheckBox) v.findViewById(R.id.checkBox1); 

        if (checkbox.isChecked() == false) {
            checkbox.setChecked(true); 
           int aaa=l.getCheckedItemPosition();

        } else {
            checkbox.setChecked(false); 
        }

    }

xml代码

主XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="5.0px"
android:paddingLeft="5.0px"
android:paddingTop="5.0px" >

<TextView
android:id="@+id/txtContactName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15.0dip"
android:layout_toLeftOf="@+id/checkBox1"
android:layout_alignParentLeft="true"
android:text="Medium Text"
android:textAppearance="?android:textAppearanceMedium" />

<TextView
android:id="@+id/txtContactNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtContactName"
android:layout_alignParentLeft="true"
android:layout_marginLeft="15.0dip"
android:layout_toLeftOf="@+id/checkBox1"
android:text="Small Text"
android:textAppearance="?android:textAppearanceSmall" />

<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false" />

</RelativeLayout>

activit_cont xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<Button
android:id="@+id/btnShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Selected" />

<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:clickable="true"
android:layout_below="@+id/btnShow" >
</ListView>
</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

首先,我想建议你应该扩展列表适配器的getView方法(阅读它),并在其中,根据我的建议做一些变化,但只是为了得到你unstuck,这是一种将其合并到您的代码中的方法。我肯定会建议使用更多描述性的变量名称:)

与此同时:

如果您想要在任何给定时间点检查过的所有人(及其编号)的列表,维护一个单独的实例变量ArrayList可能是有意义的 - 类似于{ {1}}被添加为复选框的{1}}被检查。我会创建一个名为mCheckedContacts的简单类来放入ContactInformation

ArrayList

确保实现.equals方法,因为下面的代码依赖于它(请参阅:http://developer.android.com/reference/java/util/ArrayList.html#remove(java.lang.Object)以供参考)。现在,您可以拥有以下public class ContactInformation { private String mName; private String mPhoneNumber; //make some getters and setters, a constructor, and an equals method! }

ArrayList

这可以添加到您的代码中,您可以在此列表中添加和删除联系人。

ArrayList<ContactInformation> mCheckedContactsInformation = new ArrayList<ContactInformation>();

现在您应该能够利用该ArrayList获取所需的信息。


编辑:根据您自己的回答,您可能需要对其中某些主题进行进一步说明。

:类必须具有构造函数。构造函数是实例化对象的方式。每当你拨打这样的电话时:

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
  super.onListItemClick(l, v, position, id);
  CheckBox checkbox = (CheckBox) v.findViewById(R.id.checkBox1); 
  String name = (TextView) v.findViewById(R.id.txtContactName); 
  String phoneNumber = (TextView) v.findViewById(R.id.txtContactNumber); 

  checkbox.setOnCheckedChangedListener(
    new OnCheckedChangedListener() {
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        ContactInformation curContactInfo = new ContactInformation(name, phoneNumber);
        if (isChecked) mCheckedContactsInformation.add(curContactInfo);
        //And this is why you need to implement a .equals method!
        else mCheckedContactsInformation.remove(curContactInfo);
      }
  }

  checkbox.setChecked(! checkbox.isChecked());
}

你正在调用Object o = new Object(int thisCouldBeAnyTypeOfParameter); 类中的方法,如下所示:

Object

了解更多信息(可能更好的解释)check out this info, straight from the source。所以,当你收到这个错误时:

  

构造函数ContactInformation(String,String)未定义

这可能发生在这一行:

public Object(int thisCouldBeAnyTypeOfParameter) {
  //here is some initialization code that someone wrote, or nothing...
  //but this method NEEDS to exist!

  //And if a parameter is passed, like the int above, then it is probably used to init something
}

为什么呢? 因为你没有在类中创建一个带有两个字符串的构造函数。你得到的错误实际上正是正在发生的问题。我建议你谷歌这些错误,花时间阅读你找到的结果。不幸的是,这些东西需要时间来学习,虽然有人会帮助你,但大多数工作都必须由完成,只需要花费数小时阅读和编码,阅读和编​​码。< / p>

您将收到的下一个错误:

  

类型不匹配:无法从TextView转换为String

考虑发生这种情况的代码行:

ContactInformation curContactInfo = new ContactInformation(name, phoneNumber);

左侧是String,并且需要返回String。在右侧,您没有返回String。对于这类问题,查看Android文档会很有帮助。例如,String name = (TextView) v.findViewById(R.id.txtContactName); 函数......它返回什么?您将结果类型(findViewById())转换为View,它是TextView类型的对象。这不是字符串因此您收到错误,因为您尝试将TextView对象分配给TextView。这是不合理的,因为它们是不同的类型。在这种情况下,您需要做一些额外的工作来从对象中获取String(存储在TextView对象中),以便将其分配给String。我将留下额外的工作供你确定,但是谷歌搜索“textview android”,点击第一个链接,然后查看可以在TextView上调用的所有方法。我打赌你会找到一个返回String的字段:)

希望这会有所帮助。很抱歉这么长时间啰嗦/教诲,但我只是想帮忙。

答案 1 :(得分:-1)

        String[] from = { "Name", "Phone","chkbox" };
        int[] to = { R.id.txtContactName, R.id.txtContactNumber,R.id.checkBox1 };
        ArrayList<Map<String,String>> list=buildData();
        SimpleAdapter adapter = new SimpleAdapter(this, list,
                R.layout.main, from, to);
        setListAdapter(adapter);




    }

    private ArrayList<Map<String,String>> buildData() {
        ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
        list.clear();
        Cursor people = getContentResolver().query(
                ContactsContract.Contacts.CONTENT_URI, null, null, null,
                "UPPER(" + ContactsContract.Contacts.DISPLAY_NAME + ") ASC");
        while (people.moveToNext()) {
            String contactName = people.getString(people
                    .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            String contactId = people.getString(people
                    .getColumnIndex(ContactsContract.Contacts._ID));
            String hasPhone = people
                    .getString(people
                            .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
            if ((Integer.parseInt(hasPhone) > 0)) {
                Cursor phones = getContentResolver().query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                + " = " + contactId,
                        null,
                        "UPPER(" + ContactsContract.Contacts.DISPLAY_NAME
                                + ") ASC");
                while (phones.moveToNext()) {
                    String phoneNumber = phones
                            .getString(phones
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    Map<String, String> NamePhoneType = new HashMap<String, String>();
                    NamePhoneType.put("Name", contactName);
                    NamePhoneType.put("Phone", phoneNumber);
                    list.add(NamePhoneType);
                    aa=contactName;
                     bb=phoneNumber;
                }
                phones.close();
            }
        }
        people.close();
        return list;
    }

    ArrayList<ContactInformation> mCheckedContactsInformation = new ArrayList<ContactInformation>();

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {

        super.onListItemClick(l, v, position, id);


        CheckBox checkbox = (CheckBox) v.findViewById(R.id.checkBox1); 
        // String name = (TextView) v.findViewById(R.id.txtContactName);
        TextView tvv1=(TextView) findViewById(R.id.txtContactName);
        TextView tvv2=(TextView) findViewById(R.id.txtContactNumber);
        final String name=tvv1.getText().toString();
        final String phoneNumber=tvv2.getText().toString();

checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    ContactInformation curContactInfo = new ContactInformation(name, phoneNumber);
    if (isChecked) mCheckedContactsInformation.add(curContactInfo);
    //And this is why you need to implement a .equals method!
    else mCheckedContactsInformation.remove(curContactInfo);

}
});

联系信息类代码

private String name;
private String phoneNo;
//private boolean selected;

public String getName() {
    return name;

}
public void setName(String name) {
    this.name = name;
     //selected = false;
}
public String getPhoneNo() {
    return phoneNo;
}
public void setPhoneNo(String phoneNo) {
    this.phoneNo = phoneNo;
}

/*public boolean isSelected() {
    return selected;
  }

  public void setSelected(boolean selected) {
    this.selected = selected;
  }*/

这里我得到错误 - 构造函数ContactInformation(String,String)是未定义的...当我使用String name = (TextView) v.findViewById(R.id.txtContactName);时 我得到错误 - 类型不匹配:无法从TextView转换为字符串...也可以使用我已经评论的布尔选择的方法而不是你建议的.equals方法??