Judy T Raj Judy T Raj - 21 days ago 4
Android Question

Why is my ListView using CustomAdapter not beinmg poplulated by the data I've retrieved from Firebase into an Arraylist?

I've an arraylist of objects. The data is there,I tried printing them out in a toast. But for some reason my listview remains unpopulated. I think I'm missing something obvious here. Please take a look and help me out. Thanks in advance.
Here's my code for invoking the adapter.

public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

db= FirebaseDatabase.getInstance().getReference();
listView = (ListView) getView().findViewById(R.id.listView);
items.clear();
myAdapter adapter=new myAdapter(getContext(),retreive());
listView.setAdapter(adapter);
}
public void fetchData(DataSnapshot dataSnapshot){
try {
Item i = (Item) dataSnapshot.getValue(Item.class);

items.add(i);
} catch (Exception e) {
Log.v("Error","exception",e);
}

}

public ArrayList<Item> retreive(){

db.child("Items").addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {

fetchData(dataSnapshot);
}

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
fetchData(dataSnapshot);
}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
fetchData(dataSnapshot);
}

@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
fetchData(dataSnapshot);
}

@Override
public void onCancelled(DatabaseError databaseError) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(databaseError.getMessage())
.setTitle("Error!")
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
});




return items;
}


Here is my adapter:

public class myAdapter extends BaseAdapter {
private ArrayList<Item> items;
private Context mContext;


public myAdapter(Context mContext, ArrayList<Item> items) {
this.mContext=mContext;
this.items=items;
}

@Override
public int getCount() {
return items.size();
}

@Override
public Object getItem(int i) {

return items.get(i);
}

@Override
public long getItemId(int i) {

return i;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
Item item=(Item)getItem(i);
if (view == null) {
view = LayoutInflater.from(mContext).inflate(R.layout.rowitem, viewGroup, false);
}
TextView text1=(TextView)view.findViewById(R.id.text1);
TextView text2=(TextView)view.findViewById(R.id.text2);
TextView text3=(TextView)view.findViewById(R.id.text3);
TextView text4=(TextView)view.findViewById(R.id.text4);
TextView text5=(TextView)view.findViewById(R.id.text5);
text1.setText(item.getIdno());
text2.setText(item.getName());
text3.setText(item.getBrand());
text4.setText(item.getCost());
text5.setText(item.getDate());


return view;

}
}

Answer

Replaced my custom adapter with the firebase UI List adapter and that fixed it. Thanks everyone.I'm posting my fix here in case anyone needs this.

 db= FirebaseDatabase.getInstance().getReferenceFromUrl("https://stockmanager-142503.firebaseio.com/Items");
        FirebaseListAdapter<Item> firebaseListAdapter = new FirebaseListAdapter<Item>(getActivity(),Item.class,R.layout.rowitem,db) {
            @Override
            protected void populateView(View v, Item item, int position) {
                TextView text1=(TextView)v.findViewById(R.id.text1);
                TextView text2=(TextView)v.findViewById(R.id.text2);
                TextView text3=(TextView)v.findViewById(R.id.text3);
                TextView text4=(TextView)v.findViewById(R.id.text4);
                TextView text5=(TextView)v.findViewById(R.id.text5);
                text1.setText(item.getIdno());
                text2.setText(item.getName());
                text3.setText(item.getBrand());
                text4.setText(item.getCost());
                text5.setText(item.getDate());
            }
        };
        listView = (ListView) getView().findViewById(R.id.listView);
        listView.setAdapter(firebaseListAdapter);