如何将firebase数据库中的所有项目排序到listview?我想要所有只包含共享首选项上保存的用户用户名的项目。这是我的代码如何从firebase获取项目
listView = (ListView) findViewById(R.id.lvMessages);
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list_of_rooms);
listView.setAdapter(arrayAdapter);
root.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Set<String> set = new HashSet<String>();
Iterator i = dataSnapshot.getChildren().iterator();
while(i.hasNext()){
set.add(((DataSnapshot)i.next()).getKey());
}
list_of_rooms.clear();
list_of_rooms.addAll(set);
arrayAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
答案 0 :(得分:0)
您有数据列表的本地副本。只需在本地排序。 E.g。
root.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Set<String> set = new HashSet<String>();
Iterator i = dataSnapshot.getChildren().iterator();
while(i.hasNext()){
set.add(((DataSnapshot)i.next()).getKey());
}
list_of_rooms.clear();
list_of_rooms.addAll(set);
Collections.sort(list_of_rooms, new Comparator<String>() {
@Override
public int compare(String s1, String s2)
{
return s1.compareTo(s2);
}
});
arrayAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
答案 1 :(得分:0)
while(i.hasNext()){
set.add(((DataSnapshot)i.next()).getKey());
}
list_of_rooms.clear();
list_of_rooms.addAll(set);
arrayAdapter.getFilter().filter(pref.getString("username", "").toString());
arrayAdapter.notifyDataSetChanged();
}