无法使用addValueEventListener

时间:2017-11-11 11:44:29

标签: java android firebase firebase-realtime-database

我正在尝试使用addValueEventListener检索firebase数据,但遗憾的是我无法获得正确的数据。 我有New_Deal_List.java类,在这个类中我想检索`public class New_Deal_List extends AppCompatActivity {

ListView lvDealList;

List<NewDeal_Database> dealList;

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference("Expert");

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.new__deal__list);

    lvDealList = (ListView)findViewById(R.id.lvDealList);
    dealList = new ArrayList<>();
}

@Override
protected void onStart() {
    super.onStart();

    rootRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            dealList.clear();
            for(DataSnapshot dealSnapshot : dataSnapshot.getChildren()){

                NewDeal_Database info = dealSnapshot.getValue(NewDeal_Database.class);
                dealList.add(info);
            }

            DealList adapter = new DealList( New_Deal_List.this,dealList);
            lvDealList.setAdapter(adapter);
        }


        @Override
        public void onCancelled(DatabaseError databaseError) {
            Toast.makeText(New_Deal_List.this,"Databse error",Toast.LENGTH_SHORT).show();

        }
    });
}    

我通过此方法通过New_Deal.java添加数据

private void AddNewDeal(){
    int DealName = Integer.parseInt(etDealName.getText().toString());
    String NewDealCategory = etNewDealCategory.getText().toString();
    String DishName = etDishName.getText().toString();
    String DealDescription  = etDealDescription.getText().toString();

    if(TextUtils.isEmpty(etDishName.getText().toString()) || TextUtils.isEmpty(etNewDealCategory.getText().toString()) || TextUtils.isEmpty(etDishName.getText().toString()) || TextUtils.isEmpty(etDealDescription.getText().toString())){
        Toast.makeText(this,"All fileds must be filled.",Toast.LENGTH_SHORT).show();
    }else{
        DealId = keyRefrence.push().getKey();
        //firebaseDatabase data =  new firebaseDatabase(DealName,NewDealCategory,DishName,DealDescription);

        //Contact_Info info = new Contact_Info( DealName, NewDealCategory,  DealDescription);
        NewDeal_Database info =  new NewDeal_Database(DealName,NewDealCategory, DishName,  DealDescription);      keyRefrence.child(Cooker_Deal).child(DealId).child(Deal).setValue(info);

        Toast.makeText(this,"Information Added",Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(New_Deal.this,New_Deal_Time.class);
        startActivity(intent);
    }
}

我使用此NewDeal_Databse.java设置值

public NewDeal_Database(int DealName,String NewDealCategory, String DishName, String DealDescription){
    this.DealName = DealName;
    this.NewDealCategory = NewDealCategory;
    this.DishName = DishName;
    this.DealDescription = DealDescription;
}


public int getDealName() {
    return DealName;
}

public String getNewDealCategory() {
    return NewDealCategory;
}

public String getDishName() {
    return DishName;
}

public String getDealDescription() {
    return DealDescription;
}

我也是数据适配器的DealList.java

public class DealList  extends ArrayAdapter <NewDeal_Database> {

    private Activity context;
    private List<NewDeal_Database> dealList;

    public DealList(Activity context, List<NewDeal_Database> dealList){
        super(context, R.layout.list_layout, dealList);
        this.context = context;
        this.dealList = dealList;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        LayoutInflater inflater =  context.getLayoutInflater();

        View listViewItem = inflater.inflate(R.layout.list_layout,null,true);

        TextView tvDealName = (TextView)listViewItem.findViewById(R.id.tvDealNamelayout);
        TextView tvNewDealCategory = (TextView)listViewItem.findViewById(R.id.tvNewDealCategorylayout);
        NewDeal_Database info = dealList.get(position);

        tvDealName.setText(String.valueOf(info.getDealName()));
        tvNewDealCategory.setText(info.getNewDealCategory());

        return listViewItem;
    }
}

我在输出值

上得到这些值

Output the data

我这是firebase数据库快照

firebase datasbe snapshot

updated firebase databse snapshot

Updated output

使用此代码解决了问题:

    @Override
protected void onStart() {
    super.onStart();


    //dealList.clear();

    rootRef.child(id).child(Cooker_Deal).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {


            for(DataSnapshot dealSnapshot : dataSnapshot.getChildren()){
                for(DataSnapshot datas : dealSnapshot.getChildren()){       //
                    NewDeal_Database info = datas.getValue(NewDeal_Database.class);
                    count++;
                    if(count>3){
                        dealList.add(info);
                        count=0;
                    }


                }
            }
            DealList adapter = new DealList( New_Deal_List.this,dealList);
            lvDealList.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }

2 个答案:

答案 0 :(得分:0)

我认为它的嵌套太多但是要回答你的问题,你需要从代码中删除它:

  dealList.clear();

并将其添加到addValueEventListener()

之外 像这样:

protected void onStart() {
super.onStart();
   dealList.clear();
rootRef.addValueEventListener(new ValueEventListener() {

编辑:

  @Override
 protected void onStart() {
super.onStart();
  dealList.clear();
rootRef.child(expertId).child("Cooker Deals").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {

        for(DataSnapshot dealSnapshot : dataSnapshot.getChildren()){
         for(DataSnapshot datas : dealSnapshot.getChildren(){
             NewDeal_Database info = datas.getValue(NewDeal_Database.class);
            dealList.add(info);



        }

        DealList adapter = new DealList( New_Deal_List.this,dealList);
        lvDealList.setAdapter(adapter);
        adapter.notifydatasetchanged();
    }

答案 1 :(得分:0)

假设faum-expert nide是Firebase数据库的直接子项,要获取该数据,请使用以下代码:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref = rootRef.child("faum-expert").child("Expert").child(expertId).child("Cooker Deals");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String dealName = ds.child("Deals Information").child("dealName").getValue(String.class);
            String newDealCategory = ds.child("Deals Information").child("newDealCategory").getValue(String.class);
            Log.d("TAG", dealName + " / " + newDealCategory);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
ref.addListenerForSingleValueEvent(eventListener);

输出将是:

4567 / kagj

但请注意,模型类中的字段是首字母大写,数据库中的字母是小写的第一个字母。要解决此问题,请在字段前添加以下注释,如下所示:

@PropertyName("dealName")
private String DealName;
@PropertyName("newDealCategory")
private String NewDealCategory;
@PropertyName("dishName")
private String DishName;
@PropertyName("dealDescription")
private String DealDescription;