如何避免列表视图 android 应用崩溃

How to avoid crashing for an listview android app

我在教程的帮助下构建了一个 android 应用程序。

这是MainActivity.java:-

package com.example.listdisplay;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {
    // Array of strings...
    String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry",
            "WebOS","Ubuntu","Windows7","Max OS X", "PHP", "Java", "HTML", "CSS", "JavaScript","MySQL"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ArrayAdapter adapter = new ArrayAdapter<String>(this,
                R.layout.activity_listview, mobileArray);

        ListView listView = (ListView) findViewById(R.id.mobile_list);
        listView.setAdapter(adapter);
    }
}

这是activity_main.xml:-

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ListView
        android:id="@+id/mobile_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

这是activity_listview.xml:-

<?xml version="1.0" encoding="utf-8"?>
<!--  Single List Item Design -->


<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/label"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="20dip"
    android:textSize="16dip"
    android:textStyle="bold"
    android:orientation="horizontal">
</TextView>

当我将 textview 更改为任何其他视图(如 cardview 等)时。该应用程序将崩溃。 我想用 cardview 和布局做更多的样式。就像我也尝试过 linearlayout.I 插入这个带有线性布局按钮的文本视图。但是,它仍然会崩溃。

请帮帮我。

您无法将 TextViewactivity_listview.xml 更改为因为 ArrayAdapter 构造函数需要只有 EditText.

的布局

如果你想修改它,你必须实现一个自定义 ArrayAdapter制作一个从它延伸的 class。

您可以按照下一个 Medium 教程来实现此目的。 https://medium.com/mindorks/custom-array-adapters-made-easy-b6c4930560dd

如果要添加 Button 或其他内容,则需要自定义 ArrayAdapter

如果你想用卡片视图做更多的样式,这样你就不需要将文本视图更改为卡片视图,只需像下面的代码一样编写你的代码...

<androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        app:cardCornerRadius="5dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="5dp">
            <TextView xmlns:android="http://schemas.android.com/apk/res/android"
             android:id="@+id/label"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:padding="5dip"
             android:textSize="16dip"
             android:textStyle="bold"
             android:orientation="horizontal">
             </TextView>
        </LinearLayout>
    </androidx.cardview.widget.CardView>

创建您的自定义 xml 文件:

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="8dp">

        <TextView
            android:id="@+id/label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="This is just text" />
    </LinearLayout>

</androidx.cardview.widget.CardView>

重要 部分是 TextView 中的 android:id="@+id/label"

然后将R.id.label添加到ArrayAdapter的第三个参数,像这样:

ArrayAdapter adapter = new ArrayAdapter<String>(this,
                R.layout.activity_listview, R.id.label, mobileArray);