有没有什么好的例子可以在sql查询的结果中使用expandablelistadapter?
这些文档给出了3个使用expandablelistadapter的例子,但是没有一个涉及sqlite
谢谢
凯文

最佳答案

给你。”“QuestionCategory”是我的组数据简单类。“simplequestion”用于项目数据。适配器接收上下文、MyDB适配器和一个带有组-类别的就绪ArrayList。适配器初始化其项arraylist(然后,在getchild()中,如果需要,在groupposition更改时刷新它)。不管怎样,它是适合我的需要,但任何人都可以适应他们的需要。享受。

private class QuestionCategory{
    public int id;
    public String name;

    QuestionCategory(int pId, String pName){
        this.id = pId;
        this.name = pName;
    }
}

private class SimpleQuestion extends QuestionCategory{
    public int categoryId;

    SimpleQuestion(int pCatId, int pId, String pName){
         super(pId, pName);
         categoryId = pCatId;
     }
}


private class QuestionListAdapter extends BaseExpandableListAdapter {

    private Context mContext;
    private DBAdapter mDB;

    private ArrayList<QuestionCategory> mCategoriesArrayList;
    private ArrayList<SimpleQuestion> mItemsArrayList;

    public QuestionListAdapter(Context pContext, DBAdapter pDb, ArrayList<QuestionCategory> pCategoriesArrayList) {
        mContext = pContext;
        mDB = pDb;
        mCategoriesArrayList = pCategoriesArrayList;
        mItemsArrayList = new ArrayList<SimpleQuestion>();
    }

    @Override
    public int getGroupCount() {
        return mCategoriesArrayList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        int count = 0;
        if(mItemsArrayList.isEmpty() || ((SimpleQuestion)mItemsArrayList.get(0)).categoryId != getGroupId(groupPosition)){
            Cursor itemsCursor = mDB.getQuestionsCursor((int)getGroupId(groupPosition));
            count = itemsCursor.getCount();
            itemsCursor.close();
        }
        else
            count = mItemsArrayList.size();
        return count;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return mCategoriesArrayList.get(groupPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return ((QuestionCategory)getGroup(groupPosition)).id;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        int categoryId = (int)getGroupId(groupPosition);
        //Check if we are not in our current group now, or the current cached items are wrong - MUST BE RECACHED
        if(mItemsArrayList.isEmpty() || ((SimpleQuestion)mItemsArrayList.get(0)).categoryId != categoryId){
            Cursor itemsCursor = mDB.getQuestionsCursor((int)getGroupId(groupPosition));
            itemsCursor.requery();
            mItemsArrayList.clear();
            if (itemsCursor.moveToFirst())
                do {
                    int id = itemsCursor.getInt(itemsCursor.getColumnIndex(DBAdapter.COL_ID));
                    String name = itemsCursor.getString(itemsCursor.getColumnIndex(DBAdapter.COL_TEXT));
                    SimpleQuestion newItem = new SimpleQuestion(categoryId, id, name);
                    mItemsArrayList.add(newItem);

                } while (itemsCursor.moveToNext());
            itemsCursor.close();
        }
        return mItemsArrayList.get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return ((SimpleQuestion)(getChild(groupPosition, childPosition))).id;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        LinearLayout view;
        final QuestionCategory group = (QuestionCategory)getGroup(groupPosition);
        String name = group.name;

        if (convertView == null) {
            view = new LinearLayout(mContext);
            String inflater = Context.LAYOUT_INFLATER_SERVICE;
            LayoutInflater vi = (LayoutInflater) mContext.getSystemService(inflater);
            vi.inflate(R.layout.question_list_item, view, true);
        } else {
            view = (LinearLayout) convertView;
        }

        TextView textTV = (TextView) view.findViewById(R.id.questionListItemTVText);
        textTV.setText(name);

        return view;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        LinearLayout view;
        final SimpleQuestion item = (SimpleQuestion)getChild(groupPosition, childPosition);
        String name = item.name;

        if (convertView == null) {
            view = new LinearLayout(mContext);
            String inflater = Context.LAYOUT_INFLATER_SERVICE;
            LayoutInflater vi = (LayoutInflater) mContext.getSystemService(inflater);
            vi.inflate(R.layout.question_list_item, view, true);
        } else {
            view = (LinearLayout) convertView;
        }

        TextView textTV = (TextView) view.findViewById(R.id.questionListItemTVText);
        textTV.setText(name);

        return view;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

}

10-04 14:37