如何使用 ListView 在 firebase 中检索特定变量?
How to retrieve specific variable in firebase with ListView?
使用 Firebase ListAdapter,我需要从多个节点检索数据并检索具有条件的特定变量
示例:
+-------------+--------------+
| node A | node B |
+-------------+--------------+
| name: jon | name: leen |
| age : 33 | age: 33 |
| city: amman | city: jeddah |
+-------------+--------------+
我需要检索数据 select name from npdeA, nodeB where name = "leen"
。我该怎么做?
这是我的代码
enter code here // Real Code Start Here
ListView listView = (ListView) View.findViewById(R.id.MyList);
final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReferenceFromUrl("https://assss-bf843.firebaseio.com/users");
final FirebaseListAdapter<String> firebaseListAdapter = new FirebaseListAdapter<String>(getActivity(),String.class,android.R.layout.simple_list_item_2,databaseReference) {
protected void populateView(View v, String model, int position) {
TextView textView = (TextView) v.findViewById(android.R.id.text1);
textView.setText(model);
}
}; //////////getActivity(), ApContent.class, R.layout.layout_action_plan, ref
listView.setAdapter(firebaseListAdapter);
您首先自定义 class 如:
class UserDetails {
private String name, city;
private int age;
public userDetails() {
}
public userDetails(String name, String city, int age) {
this.name = name;
this.city = city;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Q) 为什么需要创建自定义 class?
A)因为你要取多条不同类型的记录
现在在 FirebaseListAdapter 构造函数中将第二个参数作为 UserDetails.class(您的自定义 class)传递。
现在 Firebase 将获取您自定义 class (UserDetails) 中提及的完整详细信息。
现在在您的 populateView 方法中,您可以只检查名称的值是否等于 "leen"。如果是,那么您可以将其设置到您的文本视图中。
所以像这样更新您的 populateView:
protected void populateView(View v, UserDetails model, int position) {
if (model.getName().equals("leen")) {
TextView textView = (TextView) v.findViewById(android.R.id.text1);
textView.setText(model);
}
}
现在你会得到你想要的。
顺便说一句,更好的方法是创建自定义 ViewHolder class。
更新
我认为您没有在 FirebaseRecyclerAdapter
class 名称后的 angular 括号内写下正确的占位符。所以只要复制我的代码就可以了:
FirebaseRecyclerAdapter<UserDetails, View> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<UserDetails, View>(UserDetails.class, android.R.layout.simple_list_item_2, View.class, databaseReference) {
@Override
protected void populateViewHolder(View v, UserDetails model, int position) {
if (model.getName().equals("leen")) {
TextView textView = (TextView) v.findViewById(android.R.id.text1);
textView.setText(model);
}
}
};
listView.setAdapter(firebaseRecyclerAdapter);
使用 Firebase ListAdapter,我需要从多个节点检索数据并检索具有条件的特定变量
示例:
+-------------+--------------+
| node A | node B |
+-------------+--------------+
| name: jon | name: leen |
| age : 33 | age: 33 |
| city: amman | city: jeddah |
+-------------+--------------+
我需要检索数据 select name from npdeA, nodeB where name = "leen"
。我该怎么做?
这是我的代码
enter code here // Real Code Start Here
ListView listView = (ListView) View.findViewById(R.id.MyList);
final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReferenceFromUrl("https://assss-bf843.firebaseio.com/users");
final FirebaseListAdapter<String> firebaseListAdapter = new FirebaseListAdapter<String>(getActivity(),String.class,android.R.layout.simple_list_item_2,databaseReference) {
protected void populateView(View v, String model, int position) {
TextView textView = (TextView) v.findViewById(android.R.id.text1);
textView.setText(model);
}
}; //////////getActivity(), ApContent.class, R.layout.layout_action_plan, ref
listView.setAdapter(firebaseListAdapter);
您首先自定义 class 如:
class UserDetails {
private String name, city;
private int age;
public userDetails() {
}
public userDetails(String name, String city, int age) {
this.name = name;
this.city = city;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Q) 为什么需要创建自定义 class?
A)因为你要取多条不同类型的记录
现在在 FirebaseListAdapter 构造函数中将第二个参数作为 UserDetails.class(您的自定义 class)传递。
现在 Firebase 将获取您自定义 class (UserDetails) 中提及的完整详细信息。
现在在您的 populateView 方法中,您可以只检查名称的值是否等于 "leen"。如果是,那么您可以将其设置到您的文本视图中。
所以像这样更新您的 populateView:
protected void populateView(View v, UserDetails model, int position) {
if (model.getName().equals("leen")) {
TextView textView = (TextView) v.findViewById(android.R.id.text1);
textView.setText(model);
}
}
现在你会得到你想要的。
顺便说一句,更好的方法是创建自定义 ViewHolder class。
更新
我认为您没有在 FirebaseRecyclerAdapter
class 名称后的 angular 括号内写下正确的占位符。所以只要复制我的代码就可以了:
FirebaseRecyclerAdapter<UserDetails, View> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<UserDetails, View>(UserDetails.class, android.R.layout.simple_list_item_2, View.class, databaseReference) {
@Override
protected void populateViewHolder(View v, UserDetails model, int position) {
if (model.getName().equals("leen")) {
TextView textView = (TextView) v.findViewById(android.R.id.text1);
textView.setText(model);
}
}
};
listView.setAdapter(firebaseRecyclerAdapter);