我正在开发一个应用程序,其中我在ListView中有某些项目。问题是并非所有项目都在显示。即使列表滚动,其中两个也会丢失。
这些项是从firebase收集的,因此代码位于ValueEventListener中。
这是我的片段,其中显示了列表:
<FrameLayout 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="kandidat.trainingapp.Fragments.FavoritesFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/favorite_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@color/colorPrimary"
android:dividerHeight="3dp" />
</LinearLayout>
</FrameLayout>
将项目添加到适配器的代码:
FirebaseDatabase db = FirebaseDatabase.getInstance();
final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference ref = db.getReference("favorites");
DatabaseReference myRef = ref.child(user.getUid());
DatabaseReference pointRef = db.getReference("users").child(user.getUid()).child("points");
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.fragment_favorites, container, false);
listView = (ListView) rootView.findViewById(R.id.favorite_list);
LayoutInflater inflateheader = getLayoutInflater();
ViewGroup header = (ViewGroup)inflateheader.inflate(R.layout.layout_favorites_header, listView,false);
listView.addHeaderView(header);
ArrayList<FavoriteModel> allFavoriteItems = new ArrayList<>();
myRef.orderByChild("favorites").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String busStops = "Walked bus stops";
Set<FavoriteModel> bus = new TreeSet<>();
String stairs = "Walked flight of stairs";
Set<FavoriteModel> stairsList = new TreeSet<>();
String stand = "Minutes standing up";
Set<FavoriteModel> standList = new TreeSet<>();
for (DataSnapshot ds : dataSnapshot.child("BusStop").getChildren()) {
Integer dsVal = (int) (long) ds.getValue();
bus.add(new FavoriteModel(busStops, dsVal));
}
for (DataSnapshot ds : dataSnapshot.child("Stairs").getChildren()) {
Integer dsVal = (int) (long) ds.getValue();
stairsList.add(new FavoriteModel(stairs, dsVal));
}
for (DataSnapshot ds : dataSnapshot.child("standing").getChildren()) {
Integer dsVal = (int) (long) ds.getValue();
standList.add(new FavoriteModel(stand, dsVal));
}
allFavoriteItems.clear();
allFavoriteItems.addAll(bus);
allFavoriteItems.addAll(stairsList);
allFavoriteItems.addAll(standList);
FavoriteAdapter mAdapter = new FavoriteAdapter(context,
R.layout.layout_favorite_row, R.id.activity_text, allFavoriteItems);
if(allFavoriteItems.isEmpty()){
if(isAdded()) {
emptyText = (TextView) header.findViewById(R.id.empty_favorites);
emptyText.setText(getString(R.string.empty_favorites_string));
}
}else{
if(isAdded()) {
emptyText = (TextView) header.findViewById(R.id.empty_favorites);
emptyText.setText(getString(R.string.explain_favorites));
}
}
listView.setAdapter(mAdapter);
}
我希望有人知道什么是错的。如果需要更多信息或代码,请告诉我。