Firebase数据库未显示在ListView中

时间:2018-10-08 09:23:05

标签: android firebase listview firebase-realtime-database

我正在尝试从Firebase数据库获取数据。

当我尝试烤面包时,数据正在显示。但是它没有显示在我的列表视图中。

这是我班上的代码。

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

    }
});

此Toast正在运行,但列表未显示在我的列表视图中。

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

这是我的适配器

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

我尝试搜索此问题,但找不到解决方案。

希望有人可以帮助我解决这个问题。

谢谢。

2 个答案:

答案 0 :(得分:1)

使用addListenerForSingleValueEvent代替addValueEventListener

然后做

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

初始化回收者视图后, 然后

adapter.notifyDataSetChanged();

在onDataChange()上获取数据后

答案 1 :(得分:1)

您还应该在第二个notifyDataSetChanged()中呼叫onDataChange()

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

我还使用OnError,所以当问题出在其他地方时,我不会浪费时间。

此外,请注意,Toast需要一个ApplicationContext