i am having problem in retrieving more than one key by using customAdapter.
How to retrieve more than one key-value from the firebase using customAdapter
Can you share your code?
Main Activity.java
package com.example.eleventure.virtualwaiter;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.google.firebase.FirebaseApp;
import com.google.firebase.database.ChildEventListener;
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 java.util.ArrayList;
public class MainActivity extends AppCompatActivity
{
ArrayList <Order> newOrder = new ArrayList<>();
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView= findViewById(R.id.listView);
final CustomAdapter customAdapter = new CustomAdapter();
listView.setAdapter(customAdapter);
//Retrieving Data from Firebase Starts from here
FirebaseApp.initializeApp(MainActivity.this);
final DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference();
dbRef.child("coffee-time-a17ac").addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
Order data = dataSnapshot.getValue(Order.class);
newOrder.add(data);
customAdapter.notifyDataSetChanged();
}
@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) {
}
});
}
//And ends here
//Custom Array Adapter Starts Here
class CustomAdapter extends BaseAdapter{
@Override
public int getCount() {
return newOrder.size();
}
@Override
public Order getItem(int position) {
return newOrder.get(position);
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView=getLayoutInflater().inflate(
R.layout.order_item,
parent,
false
);
TextView Coffee_type=itemView.findViewById(R.id.Coffee_type);
TextView size = itemView.findViewById(R.id.size);
TextView number = itemView.findViewById(R.id.number);
Coffee_type.setText(getItem(position).getCoffee_types());
size.setText(getItem(position).getSize());
number.setText(getItem(position).getNumber());
return itemView;
}
}
//Custom Array Adapter Ends Here
}
Order.java
package com.example.eleventure.virtualwaiter;
public class Order {
String Coffee_types;
String size;
String number;
public Order() {
}
public Order(String coffee_types, String size, String number) {
Coffee_types = coffee_types;
this.size = size;
this.number = number;
}
public String getCoffee_types() {
return Coffee_types;
}
public String getSize() {
return size;
}
public String getNumber() {
return number;
}
}
The code that you have shared seems correct to me.
This is my data in database which i am trying to retrieve.
I had made several changes in code like in Order.java - I have changed the name from Coffee_types to coffeeTypes as per naming convention and adjusted the constructors and getters according to it,but it is still showing errors
Could you connect with me on TeamViewer?