首页 > 解决方案 > ListView中的Android自定义ArrayAdapter,ListView onClick适用于模拟器但不适用于手机

问题描述

我为 android 创建了一个学习助手应用程序。我使用一个自定义数组适配器,每行都有一个复选框和一个文本视图。我的一些列表视图是使用这个自定义适配器填充的。我的列表视图用于导航到其他活动,并具有 onItemClick 侦听器。

在 android studio 模拟器上,一切正常。但是,我导出了 apk 并将其安装在我的手机上,使用我的自定义适配器的列表视图在单击时不再执行任何操作。

这是我的自定义适配器:

public class CheckboxAdapter extends BaseAdapter {
    private Context context;
    ArrayList<String[]> list = new ArrayList<>();
    Boolean completed[];

    public CheckboxAdapter(ArrayList<String[]> list,Context context) {
        super();
        this.context = context;
        this.list = list;
        completed = new Boolean[list.size()];
        for (int i = 0; i < completed.length; i++){
            if(list.get(i)[1].equalsIgnoreCase("true")){
                completed[i] = true;
            } else {
                completed[i] = false;
            }
        }
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    // Class to temporarily store the textview and checkbox in each row
    public class ViewHolder {
        public TextView nametext;
        public CheckBox tick;
    }

    // Override getView method to set custom row layout
    // Use viewHolder to get the textview and checkbox then inflate it
    // Set text to passed in list of string data
    // Set checkbox to passed in array of booleans, then disable the checkbox
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder view = null;
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        if (view == null) {
            view = new ViewHolder();
            convertView = inflater.inflate(  R.layout.checkbox_adapter, null);
            view.nametext = (TextView) convertView.findViewById(R.id.adaptertextview);
            view.tick = (CheckBox)convertView.findViewById(R.id.adaptercheckbox);
            convertView.setTag(view);
        } else {
            view = (ViewHolder) convertView.getTag();
        }
        view.tick.setTag(position);
        view.nametext.setText(list.get(position)[0]);
        view.tick.setChecked(completed[position]);
        view.tick.setEnabled(false);
        return convertView;
    }
}

及其 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <CheckBox
        android:id="@+id/adaptercheckbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/adaptertextview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

下面是一个使用自定义适配器及其 onclick 函数的列表视图示例:

        ArrayList<String[]> content = new ArrayList<String[]>();
        content.add(new String[] {"Lesson 1     Highest Mark: " + highest[0], completed[0]});
        content.add(new String[] {"Lesson 2     Highest Mark: " + highest[1], completed[1]});
        content.add(new String[] {"Lesson 3     Highest Mark: " + highest[2], completed[2]});
        content.add(new String[] {"Lesson 4     Highest Mark: " + highest[3], completed[3]});
        content.add(new String[] {"Lesson 5     Highest Mark: " + highest[4], completed[4]});
        content.add(new String[] {"Combined Quiz", "false"});
        ListView quizList = (ListView) findViewById(R.id.quizListView);
        CheckboxAdapter adapter = new CheckboxAdapter(content, this);
        quizList.setAdapter(adapter);
        quizList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                System.out.println(parent. getItemAtPosition(position));
                switch (position) {
                    case 0: Intent i0 = new Intent(Quiz.this, Quiz1.class);
                        startActivity(i0);
                        break;
                    case 1: Intent i1 = new Intent(Quiz.this, Quiz2.class);
                        startActivity(i1);
                        break;
                    case 2: Intent i2 = new Intent(Quiz.this, Quiz3.class);
                        startActivity(i2);
                        break;
                    case 3: Intent i3 = new Intent(Quiz.this, Quiz4.class);
                        startActivity(i3);
                        break;
                    case 4: Intent i4 = new Intent(Quiz.this, Quiz5.class);
                        startActivity(i4);
                        break;
                    case 5: Intent i5 = new Intent(Quiz.this, QuizCombined.class);
                        startActivity(i5);
                        break;
                    default: return;
                }
            }
        });

任何想法为什么这只适用于 android studio 模拟器?我已经在两个设备和 Memu 模拟器上尝试过,但它不适用于其中任何一个,只有 android studio 模拟器。

编辑:使用上述列表视图的活动的 xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".Quiz">

        <TextView
            android:id="@+id/quizTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginStart="10dp"
            android:layout_marginTop="56dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/quizTextView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginStart="10dp"
            android:layout_marginTop="10dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/quizTextView" />

        <ListView
            android:id="@+id/quizListView"
            android:layout_width="368dp"
            android:layout_height="493dp"
            android:layout_marginLeft="10dp"
            android:layout_marginStart="10dp"
            android:layout_marginTop="10dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/quizTextView2" />

    </android.support.constraint.ConstraintLayout>

    <include
        layout="@layout/app_bar_main_menu"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main_menu"
        app:menu="@menu/activity_main_menu_drawer" />

</android.support.v4.widget.DrawerLayout>

编辑:我将项目复制到我的笔记本电脑并在那里的模拟器中运行它,但它不起作用。好像它只适用于我的电脑

标签: androidlistviewandroid-arrayadapter

解决方案


删除重复项

xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"

android.support.constraint.ConstraintLayout.

很可能是透明布局重叠并阻止点击...

inspect view boundaries按钮被钉在瓷砖上;

认为它也可以在开发人员设置中使用。


推荐阅读