Android With Listview and firebase

Is there any mistake in code because I am getting the only price in the listview and not the name can anyone help me out? I've tried so many methods to solve it but in last it let me post on StackOverflow to ask you people for the help, I am really stuck in it. I'll be really thankful to you for this help. Model class will also be provided if needed.

 private ListView mlistView;
private ArrayList<String>list;
private ArrayAdapter<String> adapter;

private ArrayList<String>Pricelist;
private ArrayAdapter<String> Priceadapter;
private DatabaseReference ref;
private FirebaseDatabase database;
private FoodItemclass fooditem;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_rice_after_login);
    final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);



    android.support.v7.app.ActionBar actionBar = getSupportActionBar();
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    actionBar.setTitle("Rice");


    fooditem = new FoodItemclass();
    mlistView = (ListView)findViewById(R.id.listview);
    database = FirebaseDatabase.getInstance();
    ref = database.getReference("Rice");
    list = new ArrayList<>();
    Pricelist = new ArrayList<>();
    adapter = new ArrayAdapter<String>(this,R.layout.list_item,R.id.ItemNameIds,list);
    Priceadapter = new ArrayAdapter<String>(this,R.layout.list_item,R.id.ItemPriceId,Pricelist);
   ref.addChildEventListener(new ChildEventListener() {
       @Override
       public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

               fooditem = dataSnapshot.getValue(FoodItemclass.class);
               list.add(fooditem.getName());
               Pricelist.add(String.valueOf(fooditem.getPrice()));
               mlistView.setAdapter(adapter);
               mlistView.setAdapter(Priceadapter);
       }

       @Override
       public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

       }

       @Override
       public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

       }

       @Override
       public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

       }

       @Override
       public void onCancelled(@NonNull DatabaseError databaseError) {

       }
   });

Upvotes: 0

Views: 73

Answers (2)

Frank van Puffelen
Frank van Puffelen

Reputation: 598765

The answer from faiizii has the gist of the problem: you can only have one adapter on a list view, so you should only call setAdapter once. Since onChildAdded will be called for each child, you shouldn't call setAdapter in there (nor in any of the other onChild... methods).

The solution is to move the calls to setAdapter out of the listener:

list = new ArrayList<>();
adapter = new ArrayAdapter<String>(this,R.layout.list_item,R.id.ItemNameIds,list);
mlistView.setAdapter(adapter);

ref.addChildEventListener(new ChildEventListener() {
   @Override
   public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
       fooditem = dataSnapshot.getValue(FoodItemclass.class);
       list.add(fooditem.getName()+": "+fooditem.getPrice());
       adapter.notifyDataSetChanged();
   }

   ...

   @Override
   public void onCancelled(@NonNull DatabaseError databaseError) {
       throw databaseError.toException();
   }
});

As you'll see we now call notifyDataSetChanged() inside onChildAdded. This lets the adapter know that its data has changed, which in turn then means that it refreshes the list view.

Upvotes: 0

Faiizii Awan
Faiizii Awan

Reputation: 1710

you can only set one adapter to your list view so thats why you get the recent assign adapter result

@Override
   public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

           fooditem = dataSnapshot.getValue(FoodItemclass.class);
           list.add(fooditem.getName()+ " "+String.valueOf(fooditem.getPrice());
           mlistView.setAdapter(adapter);
   }

NOTE: the space between both name and price value is a separator

Recommend This is a bad approach to do so. you should use RecyclerView to show multiple values

Upvotes: 1

Related Questions