我有我的ListView一个问题,即是从我的火力点获取数据。它显示的只有8.5,而不是11个项目。我不知道问题出在XML或在我的代码。我知道,这是在j变量中取11条记录,但它显示8,我的屏幕上的一半。
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
ArrayList<String> array = new ArrayList<>();
array.clear();
int j=0;
for(DataSnapshot ds : dataSnapshot.getChildren()) {
Fall fall = ds.getValue(Fall.class);
array.add("Date: " + fall.getDate() + "\n" + "Time: " + fall.getTime() + "\n" + "Location: " + fall.getLocation());
j++;
}
ArrayAdapter<String> adapter2 = new ArrayAdapter<>(History.this, android.R.layout.simple_list_item_1, array);
l1.setAdapter(adapter2);
//setListViewHeight(l1);
Toast.makeText(getApplicationContext(),String.valueOf(j), Toast.LENGTH_LONG).show();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d("FallAppError: ", databaseError.getMessage()); //errors!
}
};
uidRef.addListenerForSingleValueEvent(valueEventListener);
historyTextView.setText("Ιστορικό του/της \n" + setupDisplayName());
}
这是我的XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@mipmap/background"
android:orientation="vertical"
tools:context="com.zouve.fallapp.LoginActivity">
<TextView
android:id="@+id/historyTextView"
style="?android:textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center"
android:text="Ιστορικό"
android:textColor="@color/green"
android:textSize="25sp"
android:textStyle="bold" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/historyListView"
android:layout_width="match_parent"
android:layout_height="571dp"
android:layout_weight="1" />
</LinearLayout>
</ScrollView>
有任何想法吗?提前致谢?
让layout_height
的ListView
到wrap_content
,而不是硬编码值。
此外,@Vladyslav Matviienko是正确的,你不需要有ScrollView
作为ListView
的父母因为ListView
已经由默认的滚动功能。