Android ExpandableListAdapter and Sqlite

Are there any good examples of using an extensible adapter list with SQL query results?

The docs give 3 examples of using expandablelistadapter, but none of them apply to sqlite

thank

Kevin

+3
source share
1 answer

Here you go. "QuestionCategory" is my simple class for group data. "SimpleQuestion" is for item data. The adapter receives the Context, my DB adapter, and the finished ArrayList with groups - categories. The adapter initializes its ArrayList elements (and then, in getChild (), updates it, if necessary, when changing groupPosition). In any case, it is suitable for my needs, but everyone can adapt it for their needs. Enjoy it.

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

}
+1
source

Source: https://habr.com/ru/post/1762870/


All Articles