Android 如何将从SQLite数据库提取的组和子数据修复到可扩展的Listview
在java主活动中,我已经在initData方法中加载了数据,但是当我运行应用程序时,类别分组只列出数据库中的一个类别,每个类别列表都有datalist项。正确的功能是列出所有类别数据,然后在每个分组下列出项目。下面是我的代码 我用XML创建了一个可扩展列表视图,还创建了可扩展列表适配器类,它从SQLite db中的两个表中提取项目类别和项目类别列表。在java主活动中,我加载了数据,但是当我运行应用程序时,类别分组仅列出数据库中的一个类别和datalist项 这是我的initData方法Android 如何将从SQLite数据库提取的组和子数据修复到可扩展的Listview,android,sqlite,hashmap,expandablelistview,sqliteopenhelper,Android,Sqlite,Hashmap,Expandablelistview,Sqliteopenhelper,在java主活动中,我已经在initData方法中加载了数据,但是当我运行应用程序时,类别分组只列出数据库中的一个类别,每个类别列表都有datalist项。正确的功能是列出所有类别数据,然后在每个分组下列出项目。下面是我的代码 我用XML创建了一个可扩展列表视图,还创建了可扩展列表适配器类,它从SQLite db中的两个表中提取项目类别和项目类别列表。在java主活动中,我加载了数据,但是当我运行应用程序时,类别分组仅列出数据库中的一个类别和datalist项 这是我的initData方法
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);
}
}
}
}
这是我的可扩展列表适配器类
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;
}
}
试试这个:
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);
}
}
}
请注意,您需要更改getfpmster方法以获取参数,因为光标应该只返回特定组的数据。你也可以看看这个链接:希望有帮助 @I_A_Mok。。正如预期的那样,它工作得非常好。非常感谢你。正如您所说,我将更改getfpmster方法,使其仅为每个Listview分组获取选定的参数。非常感谢