我在pouplateViewHolder中添加了一个按钮时遇到了问题并且它没有错误,但当我按下它时,它不显示任何内容,并且它没有响应。任何人都可以帮助我,我坚持这一部分并强调它。我正在使用android studio,这是在回收者视图中。当我一直按下btn时,它不会向我显示任何内容,数据库也不会更新。
private RecyclerView mRequestsList;
private DatabaseReference mFriendsRequestDatabase;
private DatabaseReference mFriendsDatabaseRef;
private DatabaseReference mFriendsReqDatabaseRef;
private DatabaseReference mUsersDatabase;
private FirebaseAuth mAuth;
private String mCurrent_user_id;
private View mMainView;
private Button mAcceptReqBtn;
public RequestsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mMainView = inflater.inflate(R.layout.fragment_requests, container, false);
mRequestsList = (RecyclerView) mMainView.findViewById(R.id.requests_list);
mAuth = FirebaseAuth.getInstance();
mCurrent_user_id = mAuth.getCurrentUser().getUid();
mFriendsRequestDatabase = FirebaseDatabase.getInstance().getReference().child("Friend_req").child(mCurrent_user_id);;
mFriendsRequestDatabase.keepSynced(true);
mUsersDatabase = FirebaseDatabase.getInstance().getReference().child("Users");
mUsersDatabase.keepSynced(true);
mFriendsReqDatabaseRef = FirebaseDatabase.getInstance().getReference().child("Friend_req");
mFriendsDatabaseRef = FirebaseDatabase.getInstance().getReference().child("Friends");
mRequestsList.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
mRequestsList.setLayoutManager(linearLayoutManager);
mAcceptReqBtn = (Button) mMainView.findViewById(R.id.request_accept_btn);
// Inflate the layout for this fragment
return mMainView;
}
@Override
public void onStart()
{
super.onStart();
FirebaseRecyclerAdapter<Requests, RequestsFragment.RequestViewHolder>RequestsRecyclerAdapter=new FirebaseRecyclerAdapter<Requests, RequestsFragment.RequestViewHolder>
(
Requests.class,
R.layout.friend_request_single_layout,
RequestsFragment.RequestViewHolder.class,
mFriendsRequestDatabase
) {
@Override
protected void populateViewHolder(final RequestsFragment.RequestViewHolder viewHolder, Requests model, int position) {
final String list_user_id = getRef(position).getKey();
DatabaseReference get_type_ref = getRef(position).child("request_type").getRef();
get_type_ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot)
{
if(dataSnapshot.exists()){
String request_type = dataSnapshot.getValue().toString();
if(request_type.equals("received"))
{
mUsersDatabase.child(list_user_id).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
final String userName = dataSnapshot.child("name").getValue().toString();
final String userThumb = dataSnapshot.child("thumb_image").getValue().toString();
final String userStatus = dataSnapshot.child("status").getValue().toString();
viewHolder.setName(userName);
viewHolder.setUserImage(userThumb, getContext());
viewHolder.setUserStatus(userStatus);
mAcceptReqBtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
final String currentDate = DateFormat.getDateTimeInstance().format(new Date());
Map friendsMap = new HashMap();
friendsMap.put("Friends/" + mCurrent_user_id + "/" + list_user_id + "/date", currentDate);
friendsMap.put("Friends/" + list_user_id + "/" + mCurrent_user_id + "/date", currentDate);
friendsMap.put("Friend_req/" + mCurrent_user_id + "/" + list_user_id, null);
friendsMap.put("Friend_req/" + list_user_id + "/" + mCurrent_user_id, null);
mFriendsReqDatabaseRef.updateChildren(friendsMap, new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
if(databaseError == null){
Toast.makeText(getContext(),"Friends Request Accepted Successfully!", Toast.LENGTH_SHORT).show();
}
}
});
}
});
/*viewHolder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
CharSequence options[] = new CharSequence[]{"Accept Friend Request", "Cancel Friend Request"};
final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Friend Req Options");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//Click Event for each item.
if(i == 0){
final String currentDate = DateFormat.getDateTimeInstance().format(new Date());
Map friendsMap = new HashMap();
friendsMap.put("Friends/" + mCurrent_user_id + "/" + list_user_id + "/date", currentDate);
friendsMap.put("Friends/" + list_user_id + "/" + mCurrent_user_id + "/date", currentDate);
friendsMap.put("Friend_req/" + mCurrent_user_id + "/" + list_user_id, null);
friendsMap.put("Friend_req/" + list_user_id + "/" + mCurrent_user_id, null);
mFriendsReqDatabaseRef.updateChildren(friendsMap, new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
if(databaseError == null){
Toast.makeText(getContext(),"Friends Request Accepted Successfully!", Toast.LENGTH_SHORT).show();
}
}
});
}
if(i == 1){
}
}
});
builder.show();
}
});*/
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}else if(request_type.equals("sent"))
{
//req_sent_btn = (Button) mMainView.findViewById(R.id.request_accept_btn);
//req_sent_btn.setText("Request Sent");
//viewHolder.mView.findViewById(R.id.request_decline_btn).setVisibility(View.INVISIBLE);
mUsersDatabase.child(list_user_id).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
final String userName = dataSnapshot.child("name").getValue().toString();
final String userThumb = dataSnapshot.child("thumb_image").getValue().toString();
final String userStatus = dataSnapshot.child("status").getValue().toString();
viewHolder.setName(userName);
viewHolder.setUserImage(userThumb, getContext());
viewHolder.setUserStatus(userStatus);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
};
mRequestsList.setAdapter(RequestsRecyclerAdapter);
}
public static class RequestViewHolder extends RecyclerView.ViewHolder
{
View mView;
public RequestViewHolder(View itemView)
{
super(itemView);
mView=itemView;
}
public void setName(String name) {
TextView userNameView = (TextView) mView.findViewById(R.id.request_profile_name);
userNameView.setText(name);
}
public void setUserImage(String thumb_image, Context ctx) {
CircleImageView userImageView = (CircleImageView) mView.findViewById(R.id.request_profile_image);
Picasso.with(ctx).load(thumb_image).placeholder(R.drawable.default_avatar).into(userImageView);
}
public void setUserStatus(String userStatus) {
TextView user_status= (TextView) mView.findViewById(R.id.request_profile_status);
user_status.setText(userStatus);
}
}
这是XML文件。请帮忙 :(
<CircleImageView
android:id="@+id/request_profile_image"
android:layout_width="100dp"
android:layout_height="95dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:src="@drawable/default_avatar" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/request_profile_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:padding="5dp"
android:text="@string/display_name"
android:textColor="@color/colorPrimary"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="@+id/request_profile_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:padding="5dp"
android:text="User Default Status"
android:textSize="14sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<Button
android:id="@+id/request_accept_btn"
android:layout_width="90dp"
android:layout_height="40dp"
android:background="@android:color/holo_green_light"
android:text="Accept"
android:textColor="@android:color/background_light"
android:textSize="16sp" />
<Button
android:id="@+id/request_decline_btn"
android:layout_width="90dp"
android:layout_height="40dp"
android:layout_marginLeft="5dp"
android:background="@android:color/holo_red_light"
android:text="Decline"
android:textColor="@android:color/background_light"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
答案 0 :(得分:0)
如果这是您的recyclerview中的实例,则应在viewholder中初始化该按钮。希望以下代码能解决您的问题。
View mView;
Button mAcceptReqBtn;
public RequestViewHolder(View itemView)
{
super(itemView);
mView=itemView;
mAcceptReqBtn = (Button) itemView.findViewById(R.id.request_accept_btn);
}