我真的很难从数据库中收集数据。我正在使用实时数据库,我正在将值添加到可点击的列表中。
当我点击列表中的用户时,我需要获取用户名值来创建聊天ID。 获取键内的值是我的问题。他们这样做的方式似乎并没有起作用。
这是我的数据库
我需要将值给予" name"。
这是我的代码
package br.sosqueen.com.sosqueen;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Iterator;
public class UserMessageListFragment extends Fragment {
private ProgressDialog progressDialog;
private final String TAG = "UsersMessageLis";
private ArrayList<String> arrayList = new ArrayList<>();
private int totalUsers = 0;
private ListView usersList;
private TextView noUsersText;
public static UserMessageListFragment newInstance() {
return new UserMessageListFragment();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_user_message_list, container, false);
usersList = view.findViewById(R.id.usersList);
noUsersText = view.findViewById(R.id.noUsersText);
progressDialog = new ProgressDialog(getContext());
progressDialog.setMessage("Carregando...");
progressDialog.show();
//String url = "https://sosqueen-6b80b.firebaseio.com/users.json";
String url = "https://sosqueen-6b80b.firebaseio.com/usuarios.json";
StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
doOnSuccess(response);
}
}, new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "Error on trying to do request");
}
});
RequestQueue rQueue = Volley.newRequestQueue(getContext());
rQueue.add(request);
usersList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
UserDetails.chatWith = arrayList.get(position);
startActivity(new Intent(getContext(), Chat.class));
}
});
return view;
}
public void doOnSuccess(String response) {
try {
JSONObject object = new JSONObject(response);
Iterator iterator = object.keys();
String key = "";
while(iterator.hasNext()) {
key = iterator.next().toString();
if(! key.equals(UserDetails.username)) {
arrayList.add(key);
}
totalUsers++;
}
} catch (JSONException e) {
e.printStackTrace();
}
checkIfThereIsAnUser(totalUsers);
}
public void checkIfThereIsAnUser(int totalUsers) {
if(totalUsers <=1){
noUsersText.setVisibility(View.VISIBLE);
usersList.setVisibility(View.GONE);
}
else{
noUsersText.setVisibility(View.GONE);
usersList.setVisibility(View.VISIBLE);
usersList.setAdapter(new ArrayAdapter<>(getContext(), R.layout.my_list, arrayList));
}
progressDialog.dismiss();
}
}
答案 0 :(得分:0)
您想创建一个ChildEventListener并将其附加到您的&#34;用户&#34;分支。
当您首次使用ChildEventListener
连接到分支时,将为每个返回子数据分支快照的子成员触发OnChildAdded
。
就个人而言,我会存储整个数据快照结果,当在列表中呈现它时,渲染snapshot.getValue(&#34; name&#34;)而不是分支键。
否则,您需要存储分支键和名称对,然后通过DatabaseRef OnValue查询查找额外的数据信息。
或者,您可以根据某个分支值(启用.indexon
属性)创建Query,例如&#39; ciudad&#39;并根据本地名称进行过滤。