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.
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
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad