如果/ else条件具有firebase值

时间:2018-02-06 10:04:09

标签: java android android-studio if-statement logical-operators

public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.SearchViewHolder> 
{

    Context context;
    ArrayList<String> studentNameList;
    ArrayList<String> studentMatrikList;
    ArrayList<String> studentPhoneList;
    ArrayList<String> studentAddressList;
    ArrayList<String> studentStatusList;
    ArrayList<String> studentMailList;
    ArrayList<String> studentConList;
    ArrayList<String> studentIdList;

    class SearchViewHolder extends RecyclerView.ViewHolder{

       TextView studentName, studentMatrik, studentPhone, studentAddress, studentStatus, studentMail, studentCon, studentId, Check;
       CheckBox checkBox;
       Button buttonMap;
       DatabaseReference databaseReference;

       public SearchViewHolder(View itemView) {
          super(itemView);
          studentName = (TextView) itemView.findViewById(R.id.studentName);
          studentMatrik = (TextView) itemView.findViewById(R.id.studentMatrik);
          studentPhone = (TextView) itemView.findViewById(R.id.studentPhone);
          studentAddress = (TextView) itemView.findViewById(R.id.studentAddress);
          studentStatus = (TextView) itemView.findViewById(R.id.studentStatus);
          studentMail = (TextView) itemView.findViewById(R.id.studentMail);
          studentCon = (TextView) itemView.findViewById(R.id.studentCon);
          studentId = (TextView) itemView.findViewById(R.id.studentId);
          checkBox = (CheckBox) itemView.findViewById(R.id.checkBox);

          String name = studentName.getText().toString();

          if(name.equals("a")){
            checkBox.setChecked(true);
          }else{
            checkBox.setChecked(false);
          }

          buttonMap = (Button) itemView.findViewById(R.id.buttonMap);
          buttonMap.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, MapsViewActivity.class);
                intent.putExtra("Latitude",studentMail.getText().toString());
                intent.putExtra("Longitude",studentCon.getText().toString());
                intent.putExtra("Address",studentAddress.getText().toString());
                context.startActivity(intent);
            }
        });
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if (isChecked) {
                    String id = studentId.getText().toString();
                    databaseReference = FirebaseDatabase.getInstance().getReference("student");
                    String name = "Check";
                    databaseReference.child(id).child("stat").setValue(name);
                    notifyDataSetChanged();
                } else {
                    String id = studentId.getText().toString();
                    databaseReference = FirebaseDatabase.getInstance().getReference("student");
                    String name = "Not Check";
                    databaseReference.child(id).child("stat").setValue(name);
                    notifyDataSetChanged();
                }
            }
        });
    }

我从firebase到recyclerview textview itemview检索所有数据。当我在适配器中尝试if / else条件时,它会与XML中的textview android:text进行比较,而不是firebase数据值。为什么会这样?

studentName = (TextView) itemView.findViewById(R.id.studentName);

        String name = studentName.getText().toString();
        if(name.equals("a")){
            checkBox.setChecked(true);
        }else{
            checkBox.setChecked(false);
        }

它与textview android:xml中的文本而不是firebase值...

进行比较
<TextView
    android:id="@+id/studentName"
    android:layout_width="205dp"
    android:layout_height="wrap_content"
    android:text="Name goes here"
    android:textColor="#212121"
    android:textSize="15sp" />

1 个答案:

答案 0 :(得分:1)

您必须首先在textbase上设置从firebase收到的值。另外,它将继续在xml中将值设置为默认值。