一次按下后如何保存按钮状态

时间:2019-07-25 11:44:51

标签: android google-cloud-firestore recycler-adapter

我只是获取数据并将其加载到可以在recyclerView上设置的adapter中。它包含一个按钮,该按钮能够将数据发送到Firebase

  

当我按下按钮时,其color和文本会根据要求进行更改   但是每当我再次重新加载它时,默认按钮状态就会被加载。

因此,我希望该按钮在单击后应该保存其状态,并且每当我重新加载相同的活动时,它都应该处于保存状态。

      viewHolder.send_req.setOnClickListener(new View.OnClickListener() {
            @Override

            public void onClick(final View v) {
                final Integer taggedPosition =  viewHolder.getAdapterPosition();
                Log.d(TAG,"button click data:"+taggedPosition);


                firebaseFirestore.collection("Users").document(u_id).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                        if (task.getResult().exists()){
                            name = task.getResult().getString("name");
                            userID = task.getResult().getString("user_id");

                    firebaseFirestore.collection("Pets").whereEqualTo("name",name_data).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()){
                            for (final DocumentSnapshot documentSnapshot : task.getResult()){
                                docID = documentSnapshot.getId();
                                Log.d(TAG,"selected pet doc id:"+docID);
                                if (docID!= null){
                                   final Map<String, Object> userMap = new HashMap<>();
                                    final Map<String, Object> userMap1 = new HashMap<>();
                                    userMap.put("doc_id",docID);
                                    userMap.put("pet_id",pet_no);
                                    userMap.put("sender_id",userID);
                                    userMap.put("sender_name",name);
                                    userMap.put("pet_name",name_data);
                                    userMap.put("pet_profile",image_url);
                                    userMap.put("timestamp",FieldValue.serverTimestamp());

                                    //do not place request if data is already exist in document
                                    firebaseFirestore.collection("Send_req").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                                        @Override
                                        public void onComplete(@NonNull Task<QuerySnapshot> task) {
                                            List<String> list = new ArrayList<>();
                                             final Button send_req = (Button)v;
                                            if (task.isSuccessful()){
                                                for (DocumentSnapshot document : task.getResult()) {
                                                    list.add(document.getString("doc_id"));
                                                    list.add(document.getString("sender_id"));
                                                }
                                                if (!(list.contains(docID)&& list.contains(userID))){
                                                    send_req.setClickable(true);
                                                    Toast.makeText(context,"send request successfully",Toast.LENGTH_SHORT).show();
                                                    send_req.setText("Cancel Request");
                                                    send_req.setBackgroundColor(Color.RED);
                                                    String btnname =send_req.getText().toString();
                                                    String button_name = (String)userMap.put("btnName",btnname);
                                                    firebaseFirestore.collection("Send_req").add(userMap);
                                                    send_req.setClickable(false);


                                                }
                                                else {

                                                    if ((list.contains(docID)&& list.contains(userID))){
                                                    if(send_req.getText().toString().equals("Send Request") )
                                                    Toast.makeText(context,"you already send request",Toast.LENGTH_SHORT).show();
                                                    send_req.setText("Cancel Request");
                                                    send_req.setBackgroundColor(Color.RED);
                                                    send_req.setClickable(false);
                                                    notifyDataSetChanged();}
                                                  }
                                            }
                                        }
                                    });


                                }
                            }
                        }

                    }
                });
            }
        }
    });
}
});

3 个答案:

答案 0 :(得分:0)

public class SomeClass {

public static boolean isFeatureSelected = false;
...
}

您总是可以使用SomeClass.isFeatureSelected来调用它。

答案 1 :(得分:0)

创建一个布尔变量,例如:
boolean isClicked = false;int pos = -1;
然后,第一次更改按钮背景时,将布尔值更改为truepos = position;

然后在您的onBindViewHolder的{​​{1}}中执行以下操作:

RecyclerView

答案 2 :(得分:0)

如果您已经具有响应的pojo,则可以在pojo中定义一个变量,然后在该位置单击按钮时更改值。如ChrisZ所定义。

但是,如果您没有pojo,我建议您创建一个布尔值size列表,该列表与值为recyclerView item size的{​​{1}}相同。单击按钮后,使列表在该位置的布尔值变为false,这样您就可以知道您已经单击了哪个位置,并使用该位置的true值来更改UI。