lundi 18 juin 2018

Can't loop an array from another StringArray

I'm trying to set the subCategoryName in a for loop (can be seen in the first for loop in this project) from another String Array named Toiletries[] which I initialized and assigned strings to it at the top part of this project. The were no errors detected but when I run the project using Android Emulator the app keeps closing unexpectedly. But when I changed it back to the original code, it doesn't crash. The original code for the for loopsw can be found in the second for loop, "Clothes". But the original code only allow me to set the name to "Category Name" + i(loop number). Is there a way I can loop and obtain the value from another Array or how do I hard code the names like Cologne, Conditioner, into the SubCategoryName. Got the source code from a tutorial but having a problem understanding completely.

public class MainActivity extends AppCompatActivity {

private ExpandableListView lvCategory;

private ArrayList<DataItem> arCategory;
private ArrayList<SubCategoryItem> arSubCategory;
private ArrayList<ArrayList<SubCategoryItem>> arSubCategoryFinal;

private ArrayList<HashMap<String, String>> parentItems;
private ArrayList<ArrayList<HashMap<String, String>>> childItems;
private MyCategoriesExpandableListAdapter myCategoriesExpandableListAdapter;

public String[] Toiletries = {"Cologne", "Conditioner", "Cotton Buds", "Deodorant", "Hairbrush"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    setupReferences();
}

private void setupReferences() {

    lvCategory = findViewById(R.id.lvCategory);
    arCategory = new ArrayList<>();
    arSubCategory = new ArrayList<>();
    parentItems = new ArrayList<>();
    childItems = new ArrayList<>();

    DataItem dataItem = new DataItem();
    dataItem.setCategoryId("1");
    dataItem.setCategoryName("Toiletries");

    arSubCategory = new ArrayList<>();
    for(int i = 1; i < 6; i++) {

        SubCategoryItem subCategoryItem = new SubCategoryItem();
        subCategoryItem.setCategoryId(String.valueOf(i));
        subCategoryItem.setIsChecked(ConstantManager.CHECK_BOX_CHECKED_FALSE);
        subCategoryItem.setSubCategoryName(Toiletries[i]);
        arSubCategory.add(subCategoryItem);
    }




    dataItem.setSubCategory(arSubCategory);
    arCategory.add(dataItem);

    dataItem = new DataItem();
    dataItem.setCategoryId("2");
    dataItem.setCategoryName("Clothes");
    arSubCategory = new ArrayList<>();
    for(int i = 6; i < 11; i++) {

        SubCategoryItem subCategoryItem = new SubCategoryItem();
        subCategoryItem.setCategoryId(String.valueOf(i));
        subCategoryItem.setIsChecked(ConstantManager.CHECK_BOX_CHECKED_FALSE);
        subCategoryItem.setSubCategoryName("Clothes" + i);
        arSubCategory.add(subCategoryItem);
    }

    dataItem.setSubCategory(arSubCategory);
    arCategory.add(dataItem);

    dataItem = new DataItem();
    dataItem.setCategoryId("3");
    dataItem.setCategoryName("Essentials");
    arSubCategory = new ArrayList<>();
    for(int i = 11; i < 16; i++) {

        SubCategoryItem subCategoryItem = new SubCategoryItem();
        subCategoryItem.setCategoryId(String.valueOf(i));
        subCategoryItem.setIsChecked(ConstantManager.CHECK_BOX_CHECKED_FALSE);
        subCategoryItem.setSubCategoryName("Cooking: "+i);
        arSubCategory.add(subCategoryItem);
    }

    dataItem.setSubCategory(arSubCategory);
    arCategory.add(dataItem);

    Log.d("TAG", "setupReferences: "+arCategory.size());

    for(DataItem data : arCategory){

        ArrayList<HashMap<String, String>> childArrayList =new ArrayList<HashMap<String, String>>();
        HashMap<String, String> mapParent = new HashMap<String, String>();

        mapParent.put(ConstantManager.Parameter.CATEGORY_ID,data.getCategoryId());
        mapParent.put(ConstantManager.Parameter.CATEGORY_NAME,data.getCategoryName());

        int countIsChecked = 0;
        for(SubCategoryItem subCategoryItem : data.getSubCategory()) {

            HashMap<String, String> mapChild = new HashMap<String, String>();
            mapChild.put(ConstantManager.Parameter.SUB_ID,subCategoryItem.getSubId());
            mapChild.put(ConstantManager.Parameter.SUB_CATEGORY_NAME,subCategoryItem.getSubCategoryName());
            mapChild.put(ConstantManager.Parameter.CATEGORY_ID,subCategoryItem.getCategoryId());
            mapChild.put(ConstantManager.Parameter.IS_CHECKED,subCategoryItem.getIsChecked());

            if(subCategoryItem.getIsChecked().equalsIgnoreCase(ConstantManager.CHECK_BOX_CHECKED_TRUE)) {

                countIsChecked++;
            }
            childArrayList.add(mapChild);
        }

        if(countIsChecked == data.getSubCategory().size()) {

            data.setIsChecked(ConstantManager.CHECK_BOX_CHECKED_TRUE);
        }else {
            data.setIsChecked(ConstantManager.CHECK_BOX_CHECKED_FALSE);
        }

        mapParent.put(ConstantManager.Parameter.IS_CHECKED,data.getIsChecked());
        childItems.add(childArrayList);
        parentItems.add(mapParent);

    }

    ConstantManager.parentItems = parentItems;
    ConstantManager.childItems = childItems;

    myCategoriesExpandableListAdapter = new MyCategoriesExpandableListAdapter(this,parentItems,childItems,false);
    lvCategory.setAdapter(myCategoriesExpandableListAdapter);
}

}




Aucun commentaire:

Enregistrer un commentaire