ListView中的Android自定义ArrayAdapter,ListView onClick可在模拟器上使用,但不能在手机上使用

时间:2018-10-24 01:02:47

标签: android listview android-arrayadapter

我已经为Android创建了学习助手应用程序。我使用了一个自定义数组适配器,该适配器在每行中都有一个复选框和一个textview。我的某些列表视图是使用此自定义适配器填充的。我的列表视图用于导航到其他活动,并具有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>

编辑:我将项目复制到我的笔记本电脑上,并在其中的仿真器中运行,但无法正常工作。似乎只能在我的PC上使用

1 个答案:

答案 0 :(得分:1)

删除重复项

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按钮被固定在图块中;

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