我正在使用firebase并将值传递给listview
将值传递给listview我使用以下
requestList request = requestSnapshot.getValue(requestList.class);
我还需要存储记录的密钥,但它不包含在getValue调用中。
我决定为密钥编写自己的setter并将其添加到requestList类
我正在手动设置密钥
request.setrequestKey(requestSnapshot.getKey());
当我在添加请求后直接打印请求值时,我得到了正确的值
Log.i("Chat", request.getRequestTitle()+": "+request.getRequestDescription()+" "+request.getrequestKey());
返回此
RequestTitle: RequestDescription -KK4yufLTSScKHzvZtM4
然后我想在像这样的onclicklistener中使用request.getrequestkey
messagesView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
requestList o = (requestList) messagesView.getItemAtPosition(position);
String requestKey = o.getrequestKey();
Toast.makeText(getActivity(),requestKey,Toast.LENGTH_SHORT).show();
}
});
但是requestKey为null,但requestTitle和requestDescription存在且正确
我无法理解为什么班级不保留价值
任何人都可以帮忙吗
这是我的requestList类
public class requestList {
String RequestTitle;
String RequestDescription;
String requestKey;
//DataSnapshot requestSnapshot;
public requestList() {
}
public requestList(String RequestTitle, String RequestDescription) {
this.RequestTitle = RequestTitle;
this.RequestDescription = RequestDescription;
//this.key = key;
//this.requestSnapshot = requestSnapshot;
}
public String getRequestTitle() {
return RequestTitle;
}
public String getrequestKey() {
return requestKey;
}
public String getRequestDescription() {
return RequestDescription;
}
public void setrequestKey(String requestKey) {
this.requestKey = requestKey;
}
}
我现在根据评论请求包括完整的课程
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.activity_request, container, false);
final ListView messagesView = (ListView) myView.findViewById(listView);
test mApp = (test) getActivity().getApplication();
//test mApp = ((getView())getApplicationContext());
String UID = mApp.getUID();
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("users/" + UID + "/Requests" );
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot requestSnapshot: snapshot.getChildren()) {
//requestList request = requestSnapshot.getValue(requestList.class);
requestList request = requestSnapshot.getValue(requestList.class);
request.setRequestKey(requestSnapshot.getKey());
Log.i("Chat", request.getRequestTitle()+": "+request.getRequestDescription()+" "+request.getRequestKey());
}
}
@Override
public void onCancelled(DatabaseError firebaseError) {
Log.e("Chat", "The read failed: " + firebaseError.getMessage());
}
});
ListAdapter mAdapter = new FirebaseListAdapter<requestList>(getActivity(), requestList.class, android.R.layout.two_line_list_item, ref) {
@Override
protected void populateView(View view, requestList requestListItem, int position) {
((TextView)view.findViewById(android.R.id.text1)).setText(requestListItem.getRequestTitle());
((TextView)view.findViewById(android.R.id.text2)).setText(requestListItem.getRequestDescription());
}
};
messagesView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
requestList o = (requestList) messagesView.getItemAtPosition(position);
String requestKey = o.getRequestKey();
Toast.makeText(getActivity(),requestKey,Toast.LENGTH_SHORT).show();
}
});
messagesView.setAdapter(mAdapter);
return myView;
}
}
我现在包括数据库值
{
"AccountID" : "ABC123",
"Created" : "today",
"Name" : "value",
"Requests" : {
"-KK4yufLTSScKHzvZtM4" : {
"RequestDescription" : "Request Description 3",
"RequestItems" : {
"-KK4yv8qnh7YHe7WuJ8B" : {
"Description" : "Item Descriptiopn",
"Title" : "Item Title"
},
"-KK4yv8rPsnJr824rAON" : {
"Description" : "Item Descriptiopn",
"Title" : "Item Title"
},
"-KK4yv8s3ivK7Mfx166H" : {
"Description" : "Item Descriptiopn",
"Title" : "Item Title"
},
"-KK4yv8tKoM2sV1gyOkz" : {
"Description" : "Item Descriptiopn",
"Title" : "Item Title"
}
},
"RequestTitle" : "value"
}
}
}
答案 0 :(得分:0)
由于您使用的是FirebaseUI,因此您无需自行跟踪密钥。您可以使用adapter.getRef(...).getKey()
轻松获取它:
messagesView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
requestList o = (requestList) messagesView.getItemAtPosition(position);
String requestKey = adapter.getRef(position).getKey();
Toast.makeText(getActivity(),requestKey,Toast.LENGTH_SHORT).show();
}
});