从 Firebase 数据库填充 ListView
Populating ListView from Firebase Database
我有一个从 Firebase 数据库填充的 ListView:
这是我的代码:
final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReferenceFromUrl(".../comunidades");
FirebaseListAdapter<String> firebaseListAdapter = new FirebaseListAdapter<String>(
this.getActivity(),
String.class,
android.R.layout.simple_list_item_1,
databaseReference
) {
@Override
protected void populateView(View v, String model, int position) {
TextView textView = (TextView) v.findViewById(android.R.id.text1);
textView.setText(model);
mProgress.dismiss();
}
};
这是该数据库的 Firebase 控制台,分支:"comunidades":
但我在另一个片段中使用相同的代码,用同一数据库中另一个分支的对象填充列表视图,但出现错误。
这是新代码:
final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReferenceFromUrl(".../Enclaves");
FirebaseListAdapter<String> firebaseListAdapter = new FirebaseListAdapter<String>(
this.getActivity(),
String.class,
android.R.layout.simple_list_item_1,
databaseReference
) {
@Override
protected void populateView(View v, String model, int position) {
TextView textView = (TextView) v.findViewById(android.R.id.text1);
textView.setText(model);
}
};
我应该更改什么才能从分支 "Enclaves":
获取键 "Nombre_enclave" 的值
enclaves
下的不是字符串+字符串对的列表。相反,它是一个字符串+对象对的列表,每个对象都有一个 Comunidad_enclave、Descripcion_enclave 等
完成这项工作的最快方法是决定 属性 您想要显示的内容,然后覆盖 parseSnapshot()
:
FirebaseListAdapter<String> firebaseListAdapter = new FirebaseListAdapter<String>(
this.getActivity(),
String.class,
android.R.layout.simple_list_item_1,
databaseReference
) {
@Override
protected String parseSnapshot(DataSnapshot snapshot) {
return snapshot.child("Comunidad_enclave").getValue(String.class);
}
@Override
protected void populateView(View v, String model, int position) {
TextView textView = (TextView) v.findViewById(android.R.id.text1);
textView.setText(model);
}
};
解决这个问题的更好方法是创建一个代表每个社区的 class。最简单的形式可能如下所示:
class Comunidad {
public String Comunidad_enclave;
public String Descripcion__enclave;
// TODO: the same for the other properties
}
有了这个 class,您可以制作类型为 Comunidad
的适配器:
FirebaseListAdapter<Comunidad> firebaseListAdapter = new FirebaseListAdapter<Comunidad>(
this.getActivity(),
String.class,
android.R.layout.simple_list_item_2,
databaseReference
) {
@Override
protected void populateView(View v, Comunidad model, int position) {
TextView textView1 = (TextView) v.findViewById(android.R.id.text1);
textView1.setText(model.Comunidad_enclave);
TextView textView2 = (TextView) v.findViewById(android.R.id.text2);
textView2.setText(model.Descripcion_enclave);
}
};
我有一个从 Firebase 数据库填充的 ListView:
这是我的代码:
final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReferenceFromUrl(".../comunidades");
FirebaseListAdapter<String> firebaseListAdapter = new FirebaseListAdapter<String>(
this.getActivity(),
String.class,
android.R.layout.simple_list_item_1,
databaseReference
) {
@Override
protected void populateView(View v, String model, int position) {
TextView textView = (TextView) v.findViewById(android.R.id.text1);
textView.setText(model);
mProgress.dismiss();
}
};
这是该数据库的 Firebase 控制台,分支:"comunidades":
但我在另一个片段中使用相同的代码,用同一数据库中另一个分支的对象填充列表视图,但出现错误。
这是新代码:
final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReferenceFromUrl(".../Enclaves");
FirebaseListAdapter<String> firebaseListAdapter = new FirebaseListAdapter<String>(
this.getActivity(),
String.class,
android.R.layout.simple_list_item_1,
databaseReference
) {
@Override
protected void populateView(View v, String model, int position) {
TextView textView = (TextView) v.findViewById(android.R.id.text1);
textView.setText(model);
}
};
我应该更改什么才能从分支 "Enclaves":
获取键 "Nombre_enclave" 的值enclaves
下的不是字符串+字符串对的列表。相反,它是一个字符串+对象对的列表,每个对象都有一个 Comunidad_enclave、Descripcion_enclave 等
完成这项工作的最快方法是决定 属性 您想要显示的内容,然后覆盖 parseSnapshot()
:
FirebaseListAdapter<String> firebaseListAdapter = new FirebaseListAdapter<String>(
this.getActivity(),
String.class,
android.R.layout.simple_list_item_1,
databaseReference
) {
@Override
protected String parseSnapshot(DataSnapshot snapshot) {
return snapshot.child("Comunidad_enclave").getValue(String.class);
}
@Override
protected void populateView(View v, String model, int position) {
TextView textView = (TextView) v.findViewById(android.R.id.text1);
textView.setText(model);
}
};
解决这个问题的更好方法是创建一个代表每个社区的 class。最简单的形式可能如下所示:
class Comunidad {
public String Comunidad_enclave;
public String Descripcion__enclave;
// TODO: the same for the other properties
}
有了这个 class,您可以制作类型为 Comunidad
的适配器:
FirebaseListAdapter<Comunidad> firebaseListAdapter = new FirebaseListAdapter<Comunidad>(
this.getActivity(),
String.class,
android.R.layout.simple_list_item_2,
databaseReference
) {
@Override
protected void populateView(View v, Comunidad model, int position) {
TextView textView1 = (TextView) v.findViewById(android.R.id.text1);
textView1.setText(model.Comunidad_enclave);
TextView textView2 = (TextView) v.findViewById(android.R.id.text2);
textView2.setText(model.Descripcion_enclave);
}
};