Android系统。 ExpandableListAdapter和SQLite [英] Android. ExpandableListAdapter and Sqlite

查看:143
本文介绍了Android系统。 ExpandableListAdapter和SQLite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请问有什么好的例子在那里使用expandablelistadapter与SQL查询的结果呢?

are there any good examples out there of using an expandablelistadapter with the results of an sql query?

该文档提供了使用expandablelistadapter的3个例子,但它们都没有处理的SQLite

The docs give 3 examples of using expandablelistadapter, but none of them deal with sqlite

感谢

凯文

推荐答案

在这里你去。 QuestionCategory是我简单的类组数据。 SimpleQuestion对项目数据。该适配器接收背景下,我的数据库适配器和ArrayList的准备与组织 - 类别。适配器初始化其项目的ArrayList(然后,在getChild(),刷新它如果需要的话 - 当groupPosition改变)。反正它是适合我的需求,但任何人都可以适应它自己的需求。享受。

Here you go. "QuestionCategory" is my simple class for group data. "SimpleQuestion" is for item data. The adapter receives Context, my DB adapter and an ready ArrayList with groups - categories. The adapter initializes its items ArrayList (and then, in getChild(), refreshes it if needed - when groupPosition changed). 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;
    }

}

这篇关于Android系统。 ExpandableListAdapter和SQLite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆