2017-11-11 138 views
0

我試圖使用addValueEventListener檢索Firebase數據,但不幸的是我無法獲取正確的數據。 我有New_Deal_List.java類,並在這個類,我想找回'公共類New_Deal_List擴展AppCompatActivity {無法使用addValueEventListener檢索Firebase數據

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

我這是火力DATABSE快照

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(); 
     } 
+0

您想獲得交易信息下的數據嗎?你有expertId嗎? –

+0

不,我想要New_Deal_List.java類下的數據,是的,我有expertId。 –

+0

New_Deal_List.java類下有4個字段。您需要特定專家的這些值(DealName,NewDealCategory,DishName,DealDescription),這是正確的嗎? –

回答

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(); 
    } 
+0

沒有任何變化。相同的輸出。 –

+0

你想dealdescription,dealname ..? –

+0

我想要DealName和NewDealCategory。 –

0

假設faum-expert德納是你的火力地堡數據庫的直接孩子,獲得這些數據,請使用如下代碼:

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; 
+0

現在工作嗎? –

+0

我改變了你說的代碼。還要先寫小寫字母,但是我得到空白屏幕輸出。像空屏幕沒有值。 –

+0

您可以分享新代碼並添加數據庫的屏幕截圖,包括Firebase根目錄。 –