Ad

The List Values And My Given Value Is Equal Then The Checkbox Will Be Checked?

I'm working on project in android. my situation i stored list value in firebase Realtime database and then the List value and my given value is equal the checkbox will be checked.

For example: the List holding 36,40, 44.then my given value is equals to that List value then the equivalent checkbox will be checked. enter image description here

My code is

checkcheck=FirebaseDatabase.getInstance().getReference().child("Products").child(newprokey);

                checkcheck.child("size").addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {

                        final List<String> areas = new ArrayList<String>();

                        for (DataSnapshot areaSnapshot : dataSnapshot.getChildren()) {
                            String areaName = areaSnapshot.child("Value").getValue(String.class);
                            areas.add(areaName);
                        }

                        String[] check = areas.toArray(new String[areas.size()]);

                        for (int i= 0;i<check.length;i++)
                        {
                            if (check[i] == "36")
                            {
                                jS36.setChecked(true);
                            }
                            if (check[i] == "38")
                            {
                                jM38.setChecked(true);
                            }
                            if (check[i] == "40")
                            {
                                jL40.setChecked(true);
                            }

                            if (check[i] == "42")
                            {
                                jXl42.setChecked(true);
                            }

                            if (check[i] == "44")
                            {
                                jXxl44.setChecked(true);
                            }
                            if (check[i] == "46")
                            {
                                jXXXl46.setChecked(true);
                            }
                        }

                    }


                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });

i try this code. it's not working, give me solution.

Ad

Answer

You should use .equals to compare strings instead of == check this answer Also, you can traverse the List instead of creating an array.

final List<String> areas = new ArrayList<String>();

for (DataSnapshot areaSnapshot : dataSnapshot.getChildren()) {
    String areaName = areaSnapshot.child("Value").getValue(String.class);
    areas.add(areaName);
}

for (String area : areas){
    if(area.equals("36")){
        jS36.setChecked(true);
    }
    if(area.equals("38")){
        jM38.setChecked(true);
    }
    // continue
}

EDIT More simple solution is to create a Map of Checkbox and change state using the areaName directly.

// Have this Map where you initialize the checkboxes(ie after you call `findViewById` to reference the checkboxes)
HashMap<String, CheckBox> checkBoxHashMap = new HashMap<>();
checkBoxHashMap.put("36", js36);
checkBoxHashMap.put("38", jM38);
// add all the checkboxes with the areaName

for (DataSnapshot areaSnapshot : dataSnapshot.getChildren()) {
    String areaName = areaSnapshot.child("Value").getValue(String.class);
    // To make sure areaName is a valid value in the Checkbox HashMap
    if(checkBoxHashMap.containsKey(areaName)){
        checkBoxHashMap.get(areaName).setChecked(true);
    }
}
Ad
source: stackoverflow.com
Ad