android - ListView 不检查 Android 1.5 上的项目

标签 android

我正在 Eclipse 中开发适用于 Android 的应用程序。目前我的目标是 API 级别 3,但我错误地在 Android 1.6 模拟器(API 级别 4)上进行了测试。在 1.6 上它工作正常,但在 1.5 上我的带有 CHOICE_MODE_SINGLE 的 ListView 在单击时不选择项目。

这是我的 ListView XML:

<ListView 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:layout_weight="1" 
    android:id="@+id/ListDomains" 
    android:layout_margin="5px" 
    android:choiceMode="singleChoice" 
    android:clickable="false"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:descendantFocusability="beforeDescendants"
>
</ListView>

这是 ListView 中项目的 XML:

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content" 
    android:id="@+id/domain_list_value"
    android:checkMark="?android:attr/listChoiceIndicatorSingle" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:layout_width="fill_parent" 
>
</CheckedTextView>

我创建了一个自定义 ArrayList 适配器来让我自定义 getView。这是 DomainArrayAdapter 的代码:

public class DomainArrayAdapter extends ArrayAdapter<char[]> {

    private LayoutInflater mInflater;

    public DomainArrayAdapter(Context context, int textViewResourceId,
            List<char[]> objects) {     
        super(context, textViewResourceId, objects);    
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null){
            convertView = mInflater.inflate(R.layout.domain_list, null);
        }

        char[] text = super.getItem(position);

        ((CheckedTextView)convertView).setText(text, 0, text.length);
        return convertView;
    }

}

所有这些代码都可以很好地针对 API 级别 3 编译并在 Android 1.6 模拟器上运行。但是,针对 1.5 模拟器运行时,ListView 中的项目在单击时不会检查。

有什么想法吗?

最佳答案

Android 1.5 似乎不遵守 ListView XML 中设置的 choiceMode。以编程方式设置它会使其正常工作:

    ListView listDomains = (ListView) findViewById(R.id.ListDomains);

    Log.d("app", String.valueOf(listDomains.getChoiceMode())); //prints 0

    listDomains.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

    Log.d("app", String.valueOf(listDomains.getChoiceMode())); //prints 1

还有其他人见过这种行为吗?

关于android - ListView 不检查 Android 1.5 上的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3215518/

相关文章:

android - 在 MVVM 架构中用 RxJava 替换 LiveData

android - 获取 Android 设备上的未读邮件数

android - 调用 retrofit.Call<> 时出现 NullPointerException

Android 开发手册

android - 我的公共(public)变量对其堂兄是不可见的

android - Android 上 GooglePlayServicesUtil 的 NoClassDefFoundError

android - 11223056 字节分配内存不足

android - 如何在 Android 上的应用程序启动器图标上显示气球计数器

java - 确定 Executors.newSingleThreadExecutor().execute 创建的线程数?

java - 通过 bouncy caSTLe 在 android 中读取证书