Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Studio与Java:ArrayAdapter无法正常工作_Java_Android_Adapter_Android Adapter - Fatal编程技术网

Android Studio与Java:ArrayAdapter无法正常工作

Android Studio与Java:ArrayAdapter无法正常工作,java,android,adapter,android-adapter,Java,Android,Adapter,Android Adapter,我正在制作一个应用程序,用户可以在其中输入一些项目,并在添加项目时显示出来。我正在使用一个适配器来实现这一点,但适配器并没有向我显示一个包含项目的整洁列表,而是创建了一个包含多个视图的列表,这些视图重复它们自己(即,它使用该活动中的所有视图,而不是仅使用给定的TextView作为listview中条目的模板) 代码如下: 这来自createEntry类。此代码启动适配器并加载特定日期的以前条目。它会将它们全部添加到适配器中 public void loadEntries() { even

我正在制作一个应用程序,用户可以在其中输入一些项目,并在添加项目时显示出来。我正在使用一个适配器来实现这一点,但适配器并没有向我显示一个包含项目的整洁列表,而是创建了一个包含多个视图的列表,这些视图重复它们自己(即,它使用该活动中的所有视图,而不是仅使用给定的TextView作为listview中条目的模板)

代码如下:

这来自
createEntry
类。此代码启动适配器并加载特定日期的以前条目。它会将它们全部添加到适配器中

public void loadEntries() {

   events = new ArrayList<>();
   adapter = new EntryAdapter(this, entries);
   ListView listView = findViewById(R.id.entry_list);
   listView.setAdapter(adapter);

    new AsyncTask<Void, Void, List<String>>() {

        @Override
        protected List<String> doInBackground(Void... voids) {
            return appDB.entryDao().getEntriesFromDate(day, monthNum, year);
        }

        @Override
        protected void onPostExecute(List items) {
            generateEntryTable(items);
        }
    }.execute();

}

public void generateEntryTable(List items) {

    for (int i = 0; i < items.size(); i++) {
        adapter.add(new TableEntries(year, monthNum, day, entryContent));
    }

}
这是
XML
-文件:

<androidx.constraintlayout.widget.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:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CreateEntry">

<TextView
    android:id="@+id/date"
    android:layout_width="222dp"
    android:layout_height="44dp"
    android:layout_marginStart="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="24dp"
    android:layout_marginEnd="215dp"
    android:layout_marginRight="215dp"
    android:layout_marginBottom="685dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0" />

<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="403dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="116dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="200dp"
    android:background="#5CFF9800"
    android:orientation="vertical"
    app:layout_constraintBottom_toTopOf="@+id/linearLayout2"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/date"
    app:layout_constraintVertical_bias="0.0">

    <TextView
        android:id="@+id/textView3"
        android:layout_width="112dp"
        android:layout_height="35dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="276dp"
        android:layout_marginRight="276dp"
        android:layout_marginBottom="8dp"
        android:text="@string/event"
        android:textSize="24sp" />

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/addEvents"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:contentDescription="@string/add_events"
            android:cropToPadding="true"
            android:onClick="addEvent"
            android:scaleType="fitStart"
            android:tint="#FF3F02"
            app:srcCompat="@android:drawable/ic_menu_add" />

        <EditText
            android:id="@+id/addEventsPrompt"
            android:layout_width="353dp"
            android:layout_height="30dp"
            android:background="@android:color/transparent"
            android:clickable="true"
            android:freezesText="false"
            android:gravity="start|fill|center_vertical"
            android:hint="@string/startTyping"
            android:inputType="textShortMessage|textLongMessage|textMultiLine|textPersonName|text"
            android:textAllCaps="false"
            android:textAppearance="@style/TextAppearance.AppCompat.Display1"
            android:textSize="14sp" />

    </TableRow>

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

    <LinearLayout
        android:id="@+id/entry_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView

            android:id="@+id/new_entry"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content" />
    </LinearLayout>

</LinearLayout>


<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="156dp"
    android:orientation="vertical"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent">

    <TextView
        android:id="@+id/toDoTitle"
        android:layout_width="102dp"
        android:layout_height="33dp"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="322dp"
        android:layout_marginRight="322dp"
        android:text="@string/to_do"
        android:textSize="24sp" />

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageButton
            android:id="@+id/addToDo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:background="#00FFFFFF"
            android:contentDescription="@string/add_to_dos"
            android:onClick="addToDo"
            android:scaleType="fitStart"
            android:tint="#FF3B00"
            app:srcCompat="@android:drawable/ic_menu_add" />

        <EditText
            android:id="@+id/addToDoPrompt"
            android:layout_width="344dp"
            android:layout_height="32dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:layout_weight="100"
            android:autofillHints=""
            android:background="@android:color/transparent"
            android:gravity="start|fill_horizontal|center_vertical"
            android:hint="@string/startTyping"
            android:inputType="textShortMessage|textLongMessage|textMultiLine|text"
            android:textSize="14sp" />

        <TextView
            android:id="@+id/new_to_do"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </TableRow>
</LinearLayout>

如果您需要查看任何其他代码,请告诉我。
我所看到的教程中adapter用法的不同之处在于,在我的应用程序中,listview并不是活动中显示的唯一视图,因此这可能是一个问题。

您这样做的方式不对。在
入口适配器中

@Override
public View getView(int position, View convertView, ViewGroup container {

    Entry entry = getItem(position);

    if (convertView == null) {
        convertView = LayoutInflater.from(getContext())
            .inflate(R.layout.activity_create_event, container, false);
    }

...
在该
inflate
语句中,您正在为
列表视图的每一行扩展活动(
R.layout.Activity\u create\u event
)的整个XML布局。相反,您应该使用一个简单得多的布局,在一个单独的布局XML文件中只包含
TextView
,例如,让我们称之为
res/layout/list_row.XML


然后替换在
getView()
中膨胀的布局,如下所示:

@Override
public View getView(int position, View convertView, ViewGroup container {

    Entry entry = getItem(position);

    if (convertView == null) {
        convertView = LayoutInflater.from(getContext())
            .inflate(R.layout.list_row, container, false);
    }

...
我还建议您研究一下现在已经取代了
ListView
的as

@Override
public View getView(int position, View convertView, ViewGroup container {

    Entry entry = getItem(position);

    if (convertView == null) {
        convertView = LayoutInflater.from(getContext())
            .inflate(R.layout.list_row, container, false);
    }

...