Android:单击ListView中的复选框时如何获取事件?

标签 android android-listview

我是 Android 的新手,我正在尝试做一个简单的应用程序,我必须在应用程序的 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="match_parent"
    android:layout_height="match_parent"
    >

<ListView 
    android:id="@android:id/android:list" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight = "1"
    android:choiceMode="multipleChoice" android:clickable="true"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id ="@+id/savebutton"
    android:text = "save"
    />

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

<ListView 
    android:id="@android:id/android:list" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight = "1"
    android:choiceMode="multipleChoice" android:clickable="true"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id ="@+id/savebutton"
    android:text = "save"
    />

</LinearLayout>

源代码:

public class testcontacts extends ListActivity 
{
    private ListAdapter adapter = null;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //CheckBox box = (CheckBox)findViewById(R.id.checkbox);
        //box.setFocusable(false);

        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);

        adapter = new SimpleCursorAdapter
        (this, R.layout.contact_entry, cur,new String[] {ContactsContract.Contacts.DISPLAY_NAME}, new int[] {R.id.contactEntryText});   
        setListAdapter(adapter);

        ListView list = getListView();
        list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

    } 
}

最佳答案

您可以使用 getCheckedItemPosition 方法获取选中的项目的位置。一旦获得位置,您可以使用 findViewById 检索项目并使用 isChecked 方法进行检查。 这是一个例子:

     int pos = list.getCheckItemPosition(); //list being a ListView previously defined
     //Look for the checked CheckBox in the particular position
     CheckBox checkBox = (CheckBox)findViewById(pos);
     if (checkBox.isChecked()) {
     //Do something here
 }

希望对您有所帮助。

关于Android:单击ListView中的复选框时如何获取事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5764029/

相关文章:

android - Android OpenGLes LoadTextures 内存不足异常

java - 如何在android中创建自定义开关控件

android - 在 android studio 中将模块用作 jar

android - 下面带有 MPAndroidChart(或其他)的 ListView

android - 如何在屏幕上显示确切数量的 ListView 项目?

android - 房间数据库架构实体扩展错误

java - 如何使用接口(interface)从 Activity 回调回 fragment

android - 单击可单击列表时显示 ListView 项目选项

android - listview 成功运行,但在模拟器中没有任何显示

android - `ListView.invalidateViews()` 和 'Adapter.notifyDataSetChanged()' 之间有什么区别吗?