In java main activity i have loaded the data in my initData method, however when i run the app, the category grouping only lists one category from the database with the datalist items for each category listing. The correct functioning is to list all the category data and then the item lists under each grouping. Below is my code
I have created an Expandable list view in XML and also created the Expandable list Adapter class, which pulls item Category and item category list from two tables in my SQLite db . In java main activity i have loaded the data, however when i run the app, the category grouping only list one category from the database with the datalist items.
this is my initData method
private void initData() {
Cursor category1 = controller.categotyforGroupedLv();
Cursor itemListCategory = controller.getFPMsster();
listDataHeader = new ArrayList<>();
List<String> listDataItem = new ArrayList<>();
listHash = new HashMap<>();
if (category1.getCount() != 0) {
//move to the first row
category1.moveToFirst();
// Toast.makeText(this, "Data found", Toast.LENGTH_SHORT).show();
//return;
}
for (int i = 0; i < category1.getCount(); i++) {
while (category1.moveToNext()) {
while (itemListCategory.moveToNext()) {
listDataHeader.add(" " + category1.getString(1));
listDataItem.add(" " + itemListCategory.getString(2));
}
listHash.put(listDataHeader.get(0), listDataItem);
}
}
}
}
this is my expandable List Adapter class
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> listDataHeader;
private HashMap<String,List<String>> listHashMap;
public ExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listHashMap) {
this.context = context;
this.listDataHeader = listDataHeader;
this.listHashMap = listHashMap;
}
@Override
public int getGroupCount() {
return listDataHeader.size();
}
@Override
public int getChildrenCount(int i) {
return listHashMap.get(listDataHeader.get(i)).size();
}
@Override
public Object getGroup(int i) {
return listDataHeader.get(i);
}
@Override
public Object getChild(int i, int i1) {
return listHashMap.get(listDataHeader.get(i)).get(i1); //i = group item , i1 =Child item
}
@Override
public long getGroupId(int i) {
return i;
}
@Override
public long getChildId(int i, int i1) {
return i1;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
String headerTitle = (String)getGroup(i);
if (view == null)
{
LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.order_taking_screen_listgroup_for_listview, null);
}
TextView listheader1 = (TextView)view.findViewById(R.id.listheader);
listheader1.setTypeface(null, Typeface.BOLD);
listheader1.setText(headerTitle);
return view;
}
@Override
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
final String childText = (String)getChild(i, i1);
if(view == null)
{
LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.order_taking_screen_listitem_for_listview, null);
}
TextView listchild = (TextView)view.findViewById(R.id.listitem);
listchild.setText(childText);
return view;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
}
Try this:
private void initData() {
Cursor category1 = controller.categotyforGroupedLv();
Cursor itemListCategory;
listDataHeader = new ArrayList<>();
listHash = new HashMap<>();
if (category1.getCount() != 0) {
//move to the first row
category1.moveToFirst();
// Toast.makeText(this, "Data found", Toast.LENGTH_SHORT).show();
//return;
while (category1.moveToNext()) {
listDataHeader.add(" " + category1.getString(1));
// Child item should be based on group data.
itemListCategory = controller.getFPMsster(category1.getString(1));
// Create single child list.
List<String> listDataItem = new ArrayList<>();
if (itemListCategory.getCount() != 0) {
while(itemListCategory.moveToNext()) {
listDataItem.add(" " + itemListCategory.getString(2));
}
}
// Add single child into the overall child list.
listHash.put(listDataHeader.get(listDataHeader.size() - 1), listDataItem);
}
}
}
Please note that you need to change your getFPMsster() method to take an argument because the cursor should only return data for a specific group. You may also take a look of this link: GridView Within ExpandableListView With Database Hope that helps!