Reputation: 365
I try to use CheckBox
in my app. The status of checkBox
(checked/uncheked), I use in putExtra
and then in next activity, depending on the result I send a query
to database
. The problem is with multiply check.
public class Myactivity extends AppCompatActivity implements View.OnClickListener{
CheckBox mCheck1;
CheckBox mCheck2;
CheckBox mCheck3;
ImageButton mImageButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_option);
mImageButton = findViewById(R.id.button_ok_option);
mCheck1 = findViewById(R.id.checkBox1);
mCheck2 = findViewById(R.id.checkBox2);
mCheck3 = findViewById(R.id.checkBox3);
mImageButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent intent = new Intent(this, Choose_meet_activity.class);
intent.putExtra("1", mCheck1.isChecked());
intent.putExtra("2", ((mCheck1.isChecked()) && (mCheck2.isChecked()));
intent.putExtra("3", mCheck2.isChecked());
startActivity(intent)
}
}
For example, if I use mCheck1 - in database searching results with "colour":"red"
,mCheck2 - "size":"big"
. When I press the Check1 - all good, all results with "red"
come to me. And the same positive result with mCheck2, that is, under any single condition. But when I pressed mCheck1 && mCheck2 - all results with color:"red"
, even if "size"
not "big"
come back to me. What is wrong?
EDITED: Choose_meet_activity.class:
public class Choose_meet_activity extends AppCompatActivity {
public RecyclerView mResultList;
public FirebaseFirestore mFirestore;
public com.google.firebase.firestore.Query query;
public FirestoreRecyclerAdapter<Places, PlaceViewHolder> firestoreRecyclerAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycle_activity);
mResultList = findViewById(R.id.list_result);
Boolean l_check1 = getIntent().getExtras().getBoolean("1");
Boolean l_check2 = getIntent().getExtras().getBoolean("2");
Boolean l_check3 = getIntent().getExtras().getBoolean("3");
mFirestore = FirebaseFirestore.getInstance();
if (l_check1) {
query = mFirestore.collection("Places").whereEqualTo("colour", "red");
}else if (l_check2) {
query = mFirestore.collection("Places").whereEqualTo("colour", "red").whereEqualTo("size", "big");
}else if (l_check3) {
query = mFirestore.collection("Places").whereEqualTo("size", "big");
} //...
Upvotes: 1
Views: 59
Reputation: 1586
You need to choose your if-else wisely.
if (l_check2) {
query = mFirestore.collection("Places").whereEqualTo("colour", "red").whereEqualTo("size", "big");
}
else if (l_check1) {
query = mFirestore.collection("Places").whereEqualTo("colour", "red");
}
else if (l_check3) {
query = mFirestore.collection("Places").whereEqualTo("size", "big");
}
Go from multiple checked to single checked.
Upvotes: 1