ListView 布局中的复选框对于被单击的记忆非常短

发布于 2025-01-03 11:38:41 字数 1444 浏览 2 评论 0原文

下面显示联系人姓名的代码效果不错:

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class Authorize_Activity extends ListActivity {

    Cursor mContacts;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Return all contacts, ordered by name
        String[] projection = new String[] { ContactsContract.Contacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME }; // Would like the phone num, too
        mContacts = managedQuery(ContactsContract.Contacts.CONTENT_URI,
                projection, null, null, ContactsContract.Contacts.DISPLAY_NAME);

        // Display all contacts in a ListView
        SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this,
                android.R.layout.simple_list_item_multiple_choice, mContacts,
                new String[] { ContactsContract.Contacts.DISPLAY_NAME },
                new int[] { android.R.id.text1 });

        setListAdapter(mAdapter);
    }

}

..但是与姓名一起显示的复选框(通过本机 simple_list_item_multiple_choice 布局)不会保留单击 - 当您按下该复选框时,该复选框将保持选中状态,但不会保留似乎保持单击状态(要么是黑色背景下的复选框是黑色的,就像黑豹上的斑点,但我看不到它)。

我看到其他人过去也遇到过这个问题,但是与前面提到的复杂的解决方法相比,必须有一种不那么老套/笨拙的方法来解决这个问题。毕竟,如果您无法选中复选框,那么它有什么用呢?

The code below to display Contact names works somewhat well:

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class Authorize_Activity extends ListActivity {

    Cursor mContacts;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Return all contacts, ordered by name
        String[] projection = new String[] { ContactsContract.Contacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME }; // Would like the phone num, too
        mContacts = managedQuery(ContactsContract.Contacts.CONTENT_URI,
                projection, null, null, ContactsContract.Contacts.DISPLAY_NAME);

        // Display all contacts in a ListView
        SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this,
                android.R.layout.simple_list_item_multiple_choice, mContacts,
                new String[] { ContactsContract.Contacts.DISPLAY_NAME },
                new int[] { android.R.id.text1 });

        setListAdapter(mAdapter);
    }

}

..but the check boxes displayed with the names (via the native simple_list_item_multiple_choice layout) won't retain a click - the checkbox remains checked while you're pressing on it, but does not seem to remain clicked (either that, or the checkbox is black against a black background, like spots on a black panther, and I can't see it).

I see other people have had this issue in the past, too, but there HAS to be a less hacky/kludgy way of getting this to work than the complex workarounds that were mentioned. After all, what's the use of a check box if you can't check it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

む无字情书 2025-01-10 11:38:41

为了获得更好的性能和处理能力,您应该使用 BaseAdapter 类和普通 Listview。我认为当您想要自定义操作或设计时,使用 ListActivity 并不是一个好主意。这在操作和设计方面的能力都有限

已编辑

查看此链接了解适配器列表视图创意

For Better performance and handling you should use BaseAdapter class and Normal Listview.I think using ListActivity is not a good idea when you want to customize your action or design.This has limited power in terms of both action and design

Edited

See this link for adapter listview idea

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文