Android 尽管 ListView 和 SimpleAdapter 有数据,但 ListView 不显示项目
Android ListView isnt displaying items despite the ListView and SimpleAdapter having data
我不确定为什么结果没有出现在我的 ListView 中。我什至有调试日志告诉我适配器和列表视图都有 2 个项目的计数,但它仍然没有显示任何内容。这是代码
SelectPlayerActivity.java
PlayerDB db = new PlayerDB(this);
ArrayList<Player> data = db.getPlayers();
ArrayList<HashMap<String, String>> hashData = new ArrayList<HashMap<String, String>>();
for (Player player : data){
hashData.add(Player.ConvertPlayerToHashMap(player));
}
// create the resource, from, and to variables
int resource = R.layout.playerlist_row;
String[] from = {"name"};
int[] to = {R.id.playerName};
// create and set the adapter
SimpleAdapter adapter =
new SimpleAdapter(this, hashData, resource, from, to);
listPlayers.setAdapter(adapter);
adapter.notifyDataSetChanged();
// Both of these show the right amount of data, showing up as 2
Log.d("SelectPlayerActivity", "Adapter counted: " + adapter.getCount());
Log.d("SelectPlayerActivity", "ListView counted: " + listPlayers.getCount());
listPlayers.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
HashMap<String, String> hashPlayer = (HashMap<String, String>)listPlayers.getAdapter().getItem(position);
if (hashPlayer.getClass() != HashMap.class) {
Log.e("SelectPlayerActivity", "Error! hashPlayer is not a HashMap!");
}
else {
selectedPlayer = Player.ConvertHashMapToPlayer(hashPlayer);
finish();
}
}
});
我还包含了布局文件以防这些文件出现问题
activity_select_player.xml
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SelectPlayerActivity">
<TextView
android:id="@+id/lblName"
android:layout_width="79dp"
android:layout_height="43dp"
android:layout_marginStart="12dp"
android:layout_marginLeft="12dp"
android:layout_marginTop="12dp"
android:gravity="center"
android:text="Player"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnApplySelection"
android:layout_width="381dp"
android:layout_height="103dp"
android:text="Apply Selection & Return to Menu"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<EditText
android:id="@+id/txtName"
android:layout_width="292dp"
android:layout_height="45dp"
android:layout_marginTop="12dp"
android:ems="10"
android:hint="Use the list below to select a player"
android:inputType="text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.333"
app:layout_constraintStart_toEndOf="@+id/lblName"
app:layout_constraintTop_toTopOf="parent" />
<ListView
android:id="@+id/listPlayers"
android:layout_width="409dp"
android:layout_height="568dp"
android:layout_marginBottom="1dp"
app:layout_constraintBottom_toTopOf="@+id/btnApplySelection"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
playerlist_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="wrap_content">
<TextView
android:id="@+id/playerName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"/>
</LinearLayout>
EDIT1 我在下面包含了来自 hashMap 变量的数据,与从日志中复制的完全相同
hashData = {ArrayList@11212} size = 2
0 = {HashMap@11269} size = 5
"wins" -> "0"
"ties" -> "0"
"name" -> "Marky"
"id" -> "1"
"losses" -> "0"
1 = {HashMap@11270} size = 5
"wins" -> "0"
"ties" -> "0"
"name" -> "asdasd"
"id" -> "2"
"losses" -> "0"
ListView
在那里,但是因为你让它有一个固定的高度 568dp
它的两行呈现在其他 View
的“后面”,例如应用栏(在我的示例应用程序中,第二行部分可见,第一行被 TabLayout 覆盖)
我如下更改了 ListView
的约束,使其显示在 EditText
下方
<ListView
android:id="@+id/listPlayers"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="1dp"
app:layout_constraintBottom_toTopOf="@+id/btnApplySelection"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txtName"
app:layout_constraintVertical_bias="1.0" />
我不确定为什么结果没有出现在我的 ListView 中。我什至有调试日志告诉我适配器和列表视图都有 2 个项目的计数,但它仍然没有显示任何内容。这是代码
SelectPlayerActivity.java
PlayerDB db = new PlayerDB(this);
ArrayList<Player> data = db.getPlayers();
ArrayList<HashMap<String, String>> hashData = new ArrayList<HashMap<String, String>>();
for (Player player : data){
hashData.add(Player.ConvertPlayerToHashMap(player));
}
// create the resource, from, and to variables
int resource = R.layout.playerlist_row;
String[] from = {"name"};
int[] to = {R.id.playerName};
// create and set the adapter
SimpleAdapter adapter =
new SimpleAdapter(this, hashData, resource, from, to);
listPlayers.setAdapter(adapter);
adapter.notifyDataSetChanged();
// Both of these show the right amount of data, showing up as 2
Log.d("SelectPlayerActivity", "Adapter counted: " + adapter.getCount());
Log.d("SelectPlayerActivity", "ListView counted: " + listPlayers.getCount());
listPlayers.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
HashMap<String, String> hashPlayer = (HashMap<String, String>)listPlayers.getAdapter().getItem(position);
if (hashPlayer.getClass() != HashMap.class) {
Log.e("SelectPlayerActivity", "Error! hashPlayer is not a HashMap!");
}
else {
selectedPlayer = Player.ConvertHashMapToPlayer(hashPlayer);
finish();
}
}
});
我还包含了布局文件以防这些文件出现问题
activity_select_player.xml
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SelectPlayerActivity">
<TextView
android:id="@+id/lblName"
android:layout_width="79dp"
android:layout_height="43dp"
android:layout_marginStart="12dp"
android:layout_marginLeft="12dp"
android:layout_marginTop="12dp"
android:gravity="center"
android:text="Player"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnApplySelection"
android:layout_width="381dp"
android:layout_height="103dp"
android:text="Apply Selection & Return to Menu"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<EditText
android:id="@+id/txtName"
android:layout_width="292dp"
android:layout_height="45dp"
android:layout_marginTop="12dp"
android:ems="10"
android:hint="Use the list below to select a player"
android:inputType="text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.333"
app:layout_constraintStart_toEndOf="@+id/lblName"
app:layout_constraintTop_toTopOf="parent" />
<ListView
android:id="@+id/listPlayers"
android:layout_width="409dp"
android:layout_height="568dp"
android:layout_marginBottom="1dp"
app:layout_constraintBottom_toTopOf="@+id/btnApplySelection"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
playerlist_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="wrap_content">
<TextView
android:id="@+id/playerName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"/>
</LinearLayout>
EDIT1 我在下面包含了来自 hashMap 变量的数据,与从日志中复制的完全相同
hashData = {ArrayList@11212} size = 2
0 = {HashMap@11269} size = 5
"wins" -> "0"
"ties" -> "0"
"name" -> "Marky"
"id" -> "1"
"losses" -> "0"
1 = {HashMap@11270} size = 5
"wins" -> "0"
"ties" -> "0"
"name" -> "asdasd"
"id" -> "2"
"losses" -> "0"
ListView
在那里,但是因为你让它有一个固定的高度 568dp
它的两行呈现在其他 View
的“后面”,例如应用栏(在我的示例应用程序中,第二行部分可见,第一行被 TabLayout 覆盖)
我如下更改了 ListView
的约束,使其显示在 EditText
<ListView
android:id="@+id/listPlayers"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="1dp"
app:layout_constraintBottom_toTopOf="@+id/btnApplySelection"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txtName"
app:layout_constraintVertical_bias="1.0" />