安卓ListView复选框选择

3

我有一个两部分的问题。

1)如何填充我的ListView,以便显示字符串,但当选择项目时,使用的实际值是非可见id值(来自手机联系人的联系人ID)?

2)我有一个ListView,它使用多选模式进行项目选择。它由我的联系人列表中的名称填充。当我在ListView中选择一个项目时,我希望该选定项目触发对我的SqLite例程的调用,以将该值存储到数据库记录中。当在listview中选中项目时,如何使此事件触发?

这是我的布局XML;

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

<ListView
    android:id="@+id/lvContacts"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:choiceMode="multipleChoice"
/>

</LinearLayout>

这是我用来填充ListView的代码:
    private void fillData() {
    final ArrayList<String> contacts = new ArrayList<String>();

    // Let's set our local variable to a reference to our listview control
    // in the view.
    lvContacts = (ListView) findViewById(R.id.lvContacts);

    String[] proj_2 = new String[] {Data._ID, Phone.DISPLAY_NAME, CommonDataKinds.Phone.TYPE};
    cursor = managedQuery(Phone.CONTENT_URI, proj_2, null, null, null);
    while(cursor.moveToNext()) {
        // Only add contacts that have mobile number entries
        if ( cursor.getInt(2) == Phone.TYPE_MOBILE ) {
            String name = cursor.getString(1);
            contacts.add(name);
        }
    }

    // Make the array adapter for the listview.
    final ArrayAdapter<String> aa;
    aa = new ArrayAdapter<String>(this,
                                  android.R.layout.simple_list_item_multiple_choice,
                                  contacts);

    // Let's sort our resulting data alphabetically.
    aa.sort(new Comparator<String>() {
        public int compare(String object1, String object2) {
            return object1.compareTo(object2);
        };
    });

    // Give the list of contacts over to the list view now.
    lvContacts.setAdapter(aa);
}

我本希望能够为每个选中的项目使用类似于 onClick 的事件,但是一直没有任何进展。欢迎提供任何帮助,非常感谢。
1个回答

2
我认为你的问题的解决方案是使用自定义列表适配器,其中每个项目包括联系人姓名和联系人ID。以下是详细步骤:
1)尝试创建自定义联系人项bean,包括2个属性:contactID,contactName
public class contactItem{
    private long contactID;
    private String contactName;

    //...
}

创建CustomContactAdapter:
public class CustomContactAdapter extends ArrayAdapter<contactItem>{

    ArrayList<contactItem> itemList = null;

    //Constructor
    public CustomContactAdapter (Context context, int MessagewResourceId,
            ArrayList<contactItem> objects, Handler handler) {
        //Save objects and get LayoutInflater
        itemList = objects;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        final ReceiveMailStruct contact= items.get(position);

        if (contact!= null) {
            view = inflater.inflate(R.layout.layout_display_contact_item, null);
                        //Set view for contact here
         }
    }

}

搜索自定义适配器以获取更多信息

2)为了处理ListView中的点击事件,您必须为列表项注册一个处理程序: 步骤1:将处理程序注册到列表项:

lvContacts.setOnItemClickListener(new HandlerListClickEvent());

步骤2:实现项目点击时的处理过程(选中/取消选中)。
class HandlerListClickEvent implements OnItemClickListener {
    public void onItemClick( AdapterView<?> adapter, View view, int position, long id ) {
    //Get contact ID here base on item position
}

希望这能帮到您, 祝好,

nguyendat,太棒了!非常感谢你的帮助! :) - Skittles
请问您能否添加使用步骤吗? - Pankaj Kumar
1
在这种情况下,步骤2)与在ListActivity内实现方法protected void onListItemClick(ListView l, View v, int position, long id)相同,因此@nguyendat所说的内容阅读起来更“困难”。 - Caumons
+1,在我的代码中,我使用了匿名内部类作为输入参数来注册回调函数^^,你也可以使用“lvContacts.setOnItemClickListener(this)”这个方法,否则你的代码只适用于继承自ListActivity的情况。 - NguyenDat

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接