0

I am trying to retrieve firebase data using addValueEventListener, but unfortunately i am not be able to get right data. I have New_Deal_List.java class, and in this class i want to retrieve `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(); } }); } 

I am adding data through New_Deal.java by this method

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); } } 

I am setting value using this 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; } 

Also i DealList.java for array adapter

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; } } 

I get these on values on output

Output the data

I this is firebase databse snapshot

firebase datasbe snapshot

updated firebase databse snapshot

Updated output

Problem solved using this code:

 @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(); } 
4
  • You want to get the data under Deals Information? Do you have the expertId? Commented Nov 11, 2017 at 13:22
  • No i want data under New_Deal_List.java class, and yes I have expertId. Commented Nov 11, 2017 at 15:02
  • Under New_Deal_List.java class are 4 fields. You need those values (DealName, NewDealCategory, DishName,DealDescription) of a particular expert, it's correct? Commented Nov 11, 2017 at 15:09
  • I want to show DealName and NewDealCategory on listview of particular expert. Commented Nov 11, 2017 at 15:15

2 Answers 2

1

I think its nested too much but to answer your question, you need to remove this from your code:

 dealList.clear(); 

and add it outside of the addValueEventListener()

like this:

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

Edit:

 @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(); } 
Sign up to request clarification or add additional context in comments.

11 Comments

Nothing changed. Same output.
you want dealdescription, dealname..?
I want DealName and NewDealCategory.
I get this error. com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.faum.faum_expert.NewDeal_Database. at this line NewDeal_Database info = datas.getValue(NewDeal_Database.class);
try it now @ArsalanSaleem
|
0

Assuming that faum-expert nide is a direct child of your Firebase database, to get that data, please use the following code:

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); 

The output will be:

4567 / kagj 

But pay atention, the fields inside your model class are first letter uppercase and in your database are first letter lowercase. To solve this, add the following annotations in front of your fields like this:

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

6 Comments

I changed the code as you said. Also first letter to lowercase, but I get blank screen output. Like empty screen with no values.
Can you share the new code and add a screenshot of your database including the firebase root.
Is faum-expert a direct child of your Firebase database root?
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference("Expert"); thats my root refrence.
That is wrong. That's why was not working. You did not used my code. It should be getReference() without an argument.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.