片段代码
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/.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();
}
问题是,在上面提到的URL中,存在的数据是用户的唯一ID。 Inside Unique id中有4个字段 - 名称,编号,电子邮件,性别。我想只检索用户的名字我如何实现这一目标?提前致谢