0

Im trying to get data from firebase database.

The data is showing when i try to Toast it. But its not showing in my listview.

Here is the code inside my class.

mListView = (ListView) view.findViewById(R.id.mListView);
spinner = (ProgressBar)  view.findViewById(R.id.progressBar);
spinner.setVisibility(View.VISIBLE);

final ArrayList<HashMap<String, String>> arraylist = new ArrayList<HashMap<String, String>>();
final HashMap<String, String> map = new HashMap<String, String>();
Toast.makeText(getActivity(),username,Toast.LENGTH_LONG).show();
usersRef = FirebaseDatabase.getInstance().getReference("Users/"+username);
usersRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot.exists()) {

            for (DataSnapshot ds : dataSnapshot.getChildren()) {
                final String cuid = ds.child("cuid").getValue(String.class);
                String name = ds.child("cuname").getValue(String.class);
                final String newname = name.substring(0, 1).toUpperCase() + name.substring(1).toLowerCase();
                chatRef = FirebaseDatabase.getInstance().getReference("Chats").child(cuid + " " + username);
                chatRef.orderByKey().limitToLast(1).addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        for (DataSnapshot ds : dataSnapshot.getChildren()) {
                            String senderid = ds.child("senderid").getValue(String.class);
                            String message = ds.child("message").getValue(String.class);
                            String syou;
                            if (!senderid.equals(cuid)) {
                                syou = "You: ";
                            } else {
                                syou = "";
                            }
                            map.put("name", newname);
                            map.put("lastchat", syou + "" + message);
                            Toast.makeText(getActivity(),newname+" : "+message,Toast.LENGTH_LONG).show();
                            arraylist.add(map);
                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
            }

            adapter = new ListViewAdapter(getActivity(), arraylist);
            mListView.setAdapter(adapter);
            adapter.notifyDataSetChanged();
            spinner.setVisibility(View.INVISIBLE);
        }else{
            Toast.makeText(getActivity(),"NO RESULT!",Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

This Toast is working, but the list is not showing in my listview.

Toast.makeText(getActivity(),newname+" : "+message,Toast.LENGTH_LONG).show();

Here is my adapter

public class ListViewAdapter extends BaseAdapter {

    boolean expanded = false;

    Context context;
    LayoutInflater inflater;
    ArrayList<HashMap<String, String>> data;
    HashMap<String, String> resultp = new HashMap<String, String>();

    public ListViewAdapter(Context context,
                                 ArrayList<HashMap<String, String>> arraylist) {
        this.context = context;
        data = arraylist;
    }

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

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        TextView name, lastchat;

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View itemView = inflater.inflate(R.layout.item, parent, false);
        resultp = data.get(position);

        name = (TextView) itemView.findViewById(R.id.name);
        name.setText(resultp.get(FragmentAll.name));
        lastchat = (TextView) itemView.findViewById(R.id.lastchat);
        lastchat.setText(resultp.get(FragmentAll.lastchat));

        return itemView;
    }
}

I tried to search about this problem but i cant find a solution to my problem.

Hope someone can help me about this.

Thank you.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Joe
  • 387
  • 1
  • 7
  • 22
  • Try setting the `Adapter` after `arraylist.add(map);` – ʍѳђઽ૯ท Oct 08 '18 at 09:28
  • it duplicate the result. maybe because of the for loop. – Joe Oct 08 '18 at 09:31
  • Yes. Try after the loop. – ʍѳђઽ૯ท Oct 08 '18 at 09:32
  • 1
    `addValueEventListener()` is an asynchronous call, it returns immediately. While it's still in the process of fetching new DataSnapshot, you're setting the adapter with a list which doesn't have any elements in it. You should wait for the asynchronous call to finish and then set the adapter or notify data change to adapter. – Jay Oct 08 '18 at 09:33

2 Answers2

1

Use addListenerForSingleValueEvent instead of addValueEventListener .

Then do,

adapter = new ListViewAdapter(getActivity(), arraylist);
mListView.setAdapter(adapter);

after initiallizing recycler view, and then

adapter.notifyDataSetChanged();

after getting data on onDataChange()

Harpreet Singh
  • 543
  • 2
  • 10
  • 29
1

You should also call notifyDataSetChanged() in the second onDataChange():

   arraylist.add(map);
}
adapter.notifyDataSetChanged();

I also use the OnError so I don't lose time when the problem is somewhere else.

Also, please note that Toast takes an ApplicationContext.

ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
Hocine B
  • 391
  • 2
  • 8