Android的ListView的复选框选择 [英] Android ListView Checkbox Selection

查看:124
本文介绍了Android的ListView的复选框选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我种有2部分的问题在这里。

I kind of have a 2 part question here.

1)我怎么能填充我的ListView这样的字符串是显示的内容,但在选择项目时,从手机通讯录不可见的值id(接触式ID)是实际使用的价值呢?

1) How can I populate my ListView so that strings are what is displayed, but when items are selected, a non-visible id value (contact id from phone contacts) is the value that is actually used?

2)我有一个使用multipleChoice模式,项目选择一个ListView。它popluated从我的联系人列表名称。当我选择在ListView中的项目,我想这个被选中项触发打到我的SQLite常规的值存储到数据库记录。如何使这一事件时,火中的项目列表视图检查?

2) I have a ListView that's using multipleChoice mode for item selections. It's popluated with names from my contacts list. When I select an item in the ListView, I want that selected item to trigger a call to my SqLite routine to store the value into a database record. How do I make this event fire when an item is checked in the listview?

这是我的布局XML;

This is my layout 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>

这里是code我使用的填充我的ListView;

And here is the code I am using to populate my 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事件的每个检查项目,但还没有做一个位的进展。

I had hoped to be able to use something like the onClick event for each checked item, but have made not one bit of progress.

任何帮助将是所以非常AP preciated。谢谢你。

Any help would be so very appreciated. Thanks.

推荐答案

我觉得你的问题是使用自定义列表适配器女巫的每一项解决方案,包括联系人的姓名和联系方式的ID,下面是详细介绍:

I think solution for you problem is using custom list adapter witch each item include contact name and contact ID, here is detail:

1)尝试创建自定义联系人项目的bean包括2个属性:使用ContactID,联系人姓名

1) Try to create custom contact item bean include 2 properties: contactID, contactName

public class contactItem{
    private long contactID;
    private String contactName;

    //...
}

创建CustomContactAdapter:

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

}

有关详细信息,搜索自定义适配器

Search custom adapter for more information

2)到处理器Click事件在ListView控件,你必须注册一个处理程序列表项: 第一步:注册处理程序列表项:

2)To handler click event at ListView you must register an handler for listitem: Step1: Register handler to List item:

lvContacts.setOnItemClickListener(new HandlerListClickEvent());

第二步:实施proccess当项目点击(检查/取消选中)

Step2: Implement proccess when item click (check/uncheck)

class HandlerListClickEvent implements OnItemClickListener {
    public void onItemClick( AdapterView<?> adapter, View view, int position, long id ) {
    //Get contact ID here base on item position
}

希望它能帮助, 问候,

Hope it help, Regards,

这篇关于Android的ListView的复选框选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆