如何从Firebase Android中检索部分数据

时间:2018-01-02 21:41:55

标签: android database firebase firebase-realtime-database fetch

联系人片段

ListView usersList;
TextView noUsersText;
ArrayList<String> al = new ArrayList<>();
int totalUsers = 0;
ProgressDialog mProgressDialogue;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_contacts, container, false);

    usersList = (ListView)rootView.findViewById(R.id.usersList);
    noUsersText = (TextView)rootView.findViewById(R.id.noUsersText);

    mProgressDialogue = new ProgressDialog(getActivity());
    mProgressDialogue.setMessage("Loading...");
    mProgressDialogue.show();

    String url = "https://messageplus-cd647.firebaseio.com/Users.json";

    StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>(){
        @Override
        public void onResponse(String s) {
            doOnSuccess(s);
        }
    },new Response.ErrorListener(){
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            System.out.println("" + volleyError);
        }
    });

    RequestQueue rQueue = Volley.newRequestQueue(getActivity());
    rQueue.add(request);

    usersList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            UserDetails.chatWith = al.get(position);
            Intent intent = new Intent(getActivity(), Chat.class);
            startActivity(intent);
        }
    });
    return rootView;
}

doOnSuccess方法

    public void doOnSuccess(String s){
    try {
        JSONObject obj = new JSONObject(s);

        Iterator i = obj.keys();
        String key = "";

        while(i.hasNext()){
            key = i.next().toString();

            if(!key.equals(UserDetails.phonenumber)) {
                al.add(key);
            }

            totalUsers++;
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }

    if(totalUsers <=1){
        noUsersText.setVisibility(View.VISIBLE);
        usersList.setVisibility(View.GONE);
    }
    else{
        noUsersText.setVisibility(View.GONE);
        usersList.setVisibility(View.VISIBLE);
        usersList.setAdapter(new ArrayAdapter<String>(getActivity().getApplicationContext(),
                android.R.layout.simple_list_item_1 , al));
    }

    mProgressDialogue.dismiss();
}

} 我的数据库结构 - http://ibb.co/eMGhWb 我只想显示用户的名字。我如何实现这一目标。 使用当前代码,当我打开片段它只是说&#34;正在加载...&#34;并没有加载任何东西......我必须做任何事情导出json文件或任何东西?请帮助我还是初学者

1 个答案:

答案 0 :(得分:1)

要查询Firebase数据库,您只需创建一个DatabaseReference,附加一个侦听器并迭代DataSnapshot对象,如下所示:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("Users");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        ArrayList<String> arrayList = new ArrayList<>();
        ListView listView = (ListView) findViewById(R.id.list_view);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, android.R.id.text1, arrayList);

        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String name = ds.child("Name").getValue(String.class);
            arrayList.add(name);
        }
        listView.setAdapter(adapter);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
usersRef.addListenerForSingleValueEvent(eventListener);

输出只是用户名。