简体   繁体   English

Android系统。 ExpandableListAdapter和Sqlite

[英]Android. ExpandableListAdapter and Sqlite

are there any good examples out there of using an expandablelistadapter with the results of an sql query? 有没有使用expandablelistadapter和sql查询结果的好例子?

The docs give 3 examples of using expandablelistadapter, but none of them deal with sqlite 文档提供了使用expandablelistadapter的3个示例,但没有一个处理sqlite

Thanks 谢谢

Kevin 凯文

Here you go. 干得好。 "QuestionCategory" is my simple class for group data. “QuestionCategory”是我对组数据的简单类。 "SimpleQuestion" is for item data. “SimpleQuestion”用于商品数据。 The adapter receives Context, my DB adapter and an ready ArrayList with groups - categories. 适配器接收Context,我的数据库适配器和带有组 - 类别的现成ArrayList。 The adapter initializes its items ArrayList (and then, in getChild(), refreshes it if needed - when groupPosition changed). 适配器初始化其项目ArrayList(然后,在getChild()中,如果需要,在groupPosition更改时刷新它)。 Anyway it is suited for my needs, but anyone can adapt it for their needs. 无论如何它适合我的需要,但任何人都可以根据自己的需要进行调整。 Enjoy. 请享用。

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;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM