我正在使用 firebase 并将值传递给列表视图
将值传递给列表视图我使用以下
requestList request = requestSnapshot.getValue(requestList.class);
我还需要存储记录的密钥,但它不包含在 getValue 调用中。
我决定为密钥编写自己的设置器并将其添加到 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 为空,但 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"
}
}
}