Android:ArrayAdapter 仅显示一项,即使 ListView 未放置在 ScrollView 中
Android: ArrayAdapter showing only one item even though the ListView isn't placed in a ScrollView
我是 Android 的编程新手。我遇到了一个问题,我需要在列表视图中打印三个项目,但只得到一个项目。在许多答案中,我看到 ListView 不应该放在 ScrollView 中,我重新检查了我的代码,这实际上不是问题所在。
package com.apress.gerber.reminder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class RemindersActivity extends AppCompatActivity {
private ListView mListView;
private String records[] = {"first record", "second record", "third record"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reminders);
mListView = (ListView) findViewById(R.id.reminders_list_view);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
R.layout.reminders_row,
R.id.row_text,
records);
mListView.setAdapter(arrayAdapter);
}
}
Xml 个文件:-
Reminders_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="vertical">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="48dp">
<view
android:id="@+id/row_tab"
class="android.view.View"
android:layout_width="10dp"
android:layout_height="match_parent"
android:background="@android:color/holo_green_light"
/>
<TextView
android:id="@+id/row_text"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="@string/reminder"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@android:color/white"
android:textSize="18sp"
android:layout_gravity="center_vertical"
android:padding="10dp"
android:ellipsize="end"
android:maxLines="1"/>
</LinearLayout>
</LinearLayout>
activity_reminders.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
tools:context="com.apress.gerber.reminder.RemindersActivity">
<ListView
android:id="@+id/reminders_list_view"
android:layout_width="373dp"
android:layout_height="50dp"
android:background="@color/dark_grey"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp"
tools:listitem="@layout/reminders_row"/>
</RelativeLayout>
究竟发生了什么错误?
如果您不打算使用 ArrayAdapter
扩展您的 class 并自定义 getView
方法,但您想使用自定义布局,那么您的 TextView
必须具有 id android:id="@android:id/text1"
或者您可以在 ArrayAdapter 中使用 android.R.layout.simple_list_item_1
,例如:
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
records);
mListView.setAdapter(arrayAdapter);
也为了你的ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
我是 Android 的编程新手。我遇到了一个问题,我需要在列表视图中打印三个项目,但只得到一个项目。在许多答案中,我看到 ListView 不应该放在 ScrollView 中,我重新检查了我的代码,这实际上不是问题所在。
package com.apress.gerber.reminder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class RemindersActivity extends AppCompatActivity {
private ListView mListView;
private String records[] = {"first record", "second record", "third record"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reminders);
mListView = (ListView) findViewById(R.id.reminders_list_view);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
R.layout.reminders_row,
R.id.row_text,
records);
mListView.setAdapter(arrayAdapter);
}
}
Xml 个文件:- Reminders_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="vertical">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="48dp">
<view
android:id="@+id/row_tab"
class="android.view.View"
android:layout_width="10dp"
android:layout_height="match_parent"
android:background="@android:color/holo_green_light"
/>
<TextView
android:id="@+id/row_text"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="@string/reminder"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@android:color/white"
android:textSize="18sp"
android:layout_gravity="center_vertical"
android:padding="10dp"
android:ellipsize="end"
android:maxLines="1"/>
</LinearLayout>
</LinearLayout>
activity_reminders.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
tools:context="com.apress.gerber.reminder.RemindersActivity">
<ListView
android:id="@+id/reminders_list_view"
android:layout_width="373dp"
android:layout_height="50dp"
android:background="@color/dark_grey"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp"
tools:listitem="@layout/reminders_row"/>
</RelativeLayout>
究竟发生了什么错误?
如果您不打算使用 ArrayAdapter
扩展您的 class 并自定义 getView
方法,但您想使用自定义布局,那么您的 TextView
必须具有 id android:id="@android:id/text1"
或者您可以在 ArrayAdapter 中使用 android.R.layout.simple_list_item_1
,例如:
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
records);
mListView.setAdapter(arrayAdapter);
也为了你的ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"