尝试在我的项目中从Firebase DataBase获取数据。我已经能够成功检索整个对象数据(包含所有字段的数据)。我陷入困境,我试图在几个条件下检索数据。在这种情况下,尝试检索用户搜索过的名称的记录。
以下是我的Firebase数据库外观。
SearchNameActiviy.java
我从数据库中获取数据,其中我正在过滤“name”上的数据,并将其与用户从getActivity方法获取的搜索值进行比较。
package com.example.sumeet.kalwardirectorytry1;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.List;
public class SearchNameActivity extends AppCompatActivity {
String searchedValue;
private static final String TAG = "myTag";
List<Member_Pojo> memberList;
ListView listViewHome;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_name);
listViewHome = (ListView) findViewById(R.id.listView_showSearchResults);
Intent intent = getIntent();
searchedValue = intent.getStringExtra("name");
Log.e(TAG, "Searched Value" + searchedValue);
Toast.makeText(this, "Searched Value is " + searchedValue, Toast.LENGTH_SHORT).show();
memberList = new ArrayList<>();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Member");
Query query = reference.child("Member").orderByChild("name").equalTo(searchedValue);
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot memberSnapshot : dataSnapshot.getChildren()) {
Member_Pojo member = memberSnapshot.getValue(Member_Pojo.class);
Log.e(TAG, "onDataChange: " + member.getName());
memberList.add(member);
Log.e(TAG, "onDataChange: " + memberList);
}
MembersListAdapter adapter = new MembersListAdapter(SearchNameActivity.this, memberList);
listViewHome.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
MembersListAdapter.java
package com.example.sumeet.kalwardirectorytry1;
import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.List;
/**
* Created by Sumeet on 05-07-2017.
*/
public class MembersListAdapter extends ArrayAdapter<Member_Pojo> {
private Activity context;
private List<Member_Pojo> memberList;
// CustomFilter filter;
// ArrayList<Member_Pojo> filterlist;
public MembersListAdapter(Activity context, List<Member_Pojo> memberList) {
super(context, R.layout.home_listview_display, memberList);
this.context = context;
this.memberList = memberList;
//this.filterlist = (ArrayList<Member_Pojo>) memberList;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.home_listview_display, null, true);
TextView name_tv = (TextView) listViewItem.findViewById(R.id.tv_name_display);
TextView town_tv = (TextView) listViewItem.findViewById(R.id.tv_town_display);
TextView id = (TextView) listViewItem.findViewById(R.id.textViewID);
Member_Pojo member = memberList.get(position);
name_tv.setText(member.getName());
town_tv.setText(member.getTownP());
id.setText(member.getId());
return listViewItem;
}
}
Member_Pojo.java
package com.example.sumeet.kalwardirectorytry1;
/**
* Created by Sumeet on 04-07-2017.
*/
public class Member_Pojo {
private String name, addressR, townR, districtR, stateR, countryR, pincodeR, mobileNumberR, addressPersonal, townP, districtP, stateP, countryP, pincodeP, mobileNumberP, occupationP, socialOrganizationS, memberAsS, townS, stateS, countryS;
private String id;
public Member_Pojo() {
}
public Member_Pojo(String name, String addressR, String townR, String districtR, String stateR, String countryR, String pincodeR, String mobileNumberR, String addressPersonal, String townP, String districtP, String stateP, String countryP, String pincodeP, String mobileNumberP, String occupationP, String socialOrganizationS, String memberAsS, String townS, String stateS, String countryS, String id) {
this.name = name;
this.addressR = addressR;
this.townR = townR;
this.districtR = districtR;
this.stateR = stateR;
this.countryR = countryR;
this.pincodeR = pincodeR;
this.mobileNumberR = mobileNumberR;
this.addressPersonal = addressPersonal;
this.townP = townP;
this.districtP = districtP;
this.stateP = stateP;
this.countryP = countryP;
this.pincodeP = pincodeP;
this.mobileNumberP = mobileNumberP;
this.occupationP = occupationP;
this.socialOrganizationS = socialOrganizationS;
this.memberAsS = memberAsS;
this.townS = townS;
this.stateS = stateS;
this.countryS = countryS;
this.id = id;
}
public String getName() {
return name;
}
public String getAddressR() {
return addressR;
}
public String getTownR() {
return townR;
}
public String getDistrictR() {
return districtR;
}
public String getStateR() {
return stateR;
}
public String getCountryR() {
return countryR;
}
public String getPincodeR() {
return pincodeR;
}
public String getMobileNumberR() {
return mobileNumberR;
}
public String getAddressPersonal() {
return addressPersonal;
}
public String getTownP() {
return townP;
}
public String getDistrictP() {
return districtP;
}
public String getStateP() {
return stateP;
}
public String getCountryP() {
return countryP;
}
public String getPincodeP() {
return pincodeP;
}
public String getMobileNumberP() {
return mobileNumberP;
}
public String getOccupationP() {
return occupationP;
}
public String getSocialOrganizationS() {
return socialOrganizationS;
}
public String getMemberAsS() {
return memberAsS;
}
public String getTownS() {
return townS;
}
public String getStateS() {
return stateS;
}
public String getCountryS() {
return countryS;
}
public String getId() {
return id;
}
}
请你纠正我的错误吗? 目前我没有收到任何错误,但列表视图没有显示任何数据。 我猜我在查询数据库时出错了。 在此先感谢。
答案 0 :(得分:0)
你们都有getReference("Member")
和child("Member"
)。删除后者或使用getReference("Member")
getReference()
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Member");
Query query = reference.child("Member").orderByChild("name").equalTo(searchedValue);