0

资深极客。

我想请求一个简单但完全有效的示例,说明如何在从 BaseExpandableListAdapter 扩展的同时实现 ExpandableListView 然而从 Sqlite 数据库中读取数据。

我已经对这个问题进行了研究和实验(请参见此处),但是在我能够在标题中显示一些数据的地方取得的成功很少,尽管所有组标题的重复值相同。子项也不显示。

使用 BaseExpandableListAdapter 进行扩展的原因是为组标题提供自定义布局。SQLite 访问的原因自然是因为那是我的数据存储的地方。

到目前为止,在网上搜索到的所有示例都使用 SimpleCursorTreeAdapter 或 CursorTreeAdapter 作为基于 DB 的应用程序中的扩展程序,或者在使用的数据位于 ArrayLists 中时仅使用 BaseExpandableListAdapter。

以下是迄今为止的实验。(使用此代码,只有组标题一遍又一遍地填充相同的数字。子项不会出现)

public class ExpandableListViewAdapterCustom extends BaseExpandableListAdapter {
    protected Activity currentActivity;
    public ExpandableListViewAdapterCustom(Activity callingActivity){
    this.currentActivity = callingActivity;
    }

    private Cursor mGroupsCursorLocal ;
    private Cursor mChildCursor;
    private Context ctx;
    private int groupItem;
    private int childItem;
    private String[] fieldsToUseFromGroupCursor;
    private int[] screenTextsToMapGroupDataTo;
    private String[] fieldsToUseFromChildCursor;
    private int[] screenTextsToMapChildDataTo;

    public ArrayList<String> tempChild;
    public LayoutInflater minflater;
    public Activity activity;
    public int intGroupTotal;


    public void setCurrentActivity(Activity activity) {
       this.activity = activity;
    }

    public void setCtx(Context ctx) {
       this.ctx = ctx;
    }

     public void setGroupItem(int groupItem) {
         this.groupItem = groupItem;
      }

    public void setChildItem(int childItem) {
        this.childItem = childItem;
    }

    public Activity getCurrentActivity() {
        return currentActivity;
    }

     public Cursor getmGroupsCursorLocal() {
        return mGroupsCursorLocal;
      }

    public Context getCtx() {
        return currentActivity.getBaseContext();
    }

    public void setmGroupsCursorLocal(Cursor mGroupsCursor) {
        this.mGroupsCursorLocal = mGroupsCursor;
    }

    public ExpandableListViewAdapterCustom(Cursor mGroupsCursor,
                                       Activity activity,
                                       int groupItem,
                                       int childItem,
                                       String[] fieldsToUseFromGroupCursor,
                                       int[] screenTextsToMapGroupDataTo,
                                       String[] fieldsToUseFromChildCursor,
                                       int[] screenTextsToMapChildDataTo) {



        DatabaseRoutines db = new DatabaseRoutines(activity);

        setmGroupsCursorLocal(mGroupsCursor);
        mGroupsCursorLocal = db.fetchGroup();
        activity.startManagingCursor (mGroupsCursor);
        mGroupsCursorLocal.moveToFirst();

        mChildCursor=db.fetchChildren(mGroupsCursorLocal.getColumnIndex("Year"));
        mChildCursor.moveToFirst();
        activity.startManagingCursor(mChildCursor);



        setCtx(activity);
        setCurrentActivity(activity);

        }

        public void setInflater(LayoutInflater mInflater, Activity act) {
        this.minflater = mInflater;
        activity = act;
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
           return null;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
          return 0;
        }


        @Override
        public View getChildView(int groupPosition, 
            int childPosition,boolean         
            isLastChild, 
            View convertView, 
            ViewGroup parent) {
           View v = convertView;
           if (v == null) 
        {
        LayoutInflater inflater = 
       (LayoutInflater)     ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);            
       v = inflater.inflate(R.layout.exp_listview_childrow, parent, false);
        }

        TextView txtMonth = (TextView) v.findViewById(R.id.txtMonth);
        TextView txtMonthAmountSent = (TextView)
        v.findViewById(R.id.txtMonthAmountSentValue);
        TextView txtMonthReceived = (TextView)
        v.findViewById(R.id.txtMonthAmountReceivedValue);
        txtMonth.setText(mChildCursor.getString(mChildCursor.getColumnIndex("Month")));

    txtMonthAmountSent.setText
   (mChildCursor.getString(mChildCursor.getColumnIndex("TotalSent")));
    txtMonthReceived.setText

    (mChildCursor.getString(mChildCursor.getColumnIndex("TotalReceived")));
    return v;
     }


    @Override
    public int getChildrenCount(int groupPosition) {
        return (mChildCursor.getCount());
       }

    @Override
     public Object getGroup(int groupPosition) {
     return null;
    }

    @Override
    public int getGroupCount() {
    return mGroupsCursorLocal.getCount();
    }

    @Override
    public void onGroupCollapsed(int groupPosition) {
    super.onGroupCollapsed(groupPosition);
    }

    @Override
     public void onGroupExpanded(int groupPosition) {
    super.onGroupExpanded(groupPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
    return 0;
    }


    @Override
    public View getGroupView(
       int groupPosition, 
       boolean isExpanded,
       View convertView,     
       ViewGroup parent) 
     {
         View v = convertView;
         if (v == null) {
        LayoutInflater inflater =  
        (LayoutInflater)  ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.exp_listview_groupheader, parent, false);
       }

       TextView txtYear = (TextView) v.findViewById(R.id.txtYearValue);
       TextView txtAmountSent = (TextView) v.findViewById(R.id.txtAmountSentValue);
       TextView txtAmountRecieved = (TextView) 
       v.findViewById(R.id.txtAmountReceivedValue);

       txtYear.setText(mGroupsCursorLocal.getString(
       mGroupsCursorLocal.getColumnIndex("Year")));

       txtAmountSent.setText(
       mGroupsCursorLocal.getString(mGroupsCursorLocal.getColumnIndex("TotalSent")));

       txtAmountRecieved.setText(
      GroupsCursorLocal.getString(mGroupsCursorLocal.getColumnIndex("TotalReceived")));
      return v;
    }

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

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


}

数据库代码是这样的

public Cursor fetchGroup() {
    SQLiteDatabase db = this.getReadableDatabase();  //if memory leaks check here
        String query = "SELECT DISTINCT MIN(ID) AS id, 
    Year, SUM(SentAmount) AS    TotalSent, SUM(ReceivedAmount) AS TotalReceived 
     FROM  MyTbl GROUP BY Year ORDER BY Year DESC ";
      return db.rawQuery(query, null);}

public Cursor fetchChildren(int Yr) {
    SQLiteDatabase db = this.getReadableDatabase(); //if memory leaks check here
    String query = "SELECT  MIN(ID) AS id, 
    Year, Month, SUM(SentAmount) AS TotalSent, 
     SUM(ReceivedAmount) AS TotalReceived        
     FROM  MyTbl Where Year= "+ Yr +"   GROUP BY Year, 
     Month ORDER BY Year DESC, Month DESC";
    return db.rawQuery(query, null);
   } 

使用以下方法从主要活动调用代码

    ExpandableListView elv = (ExpandableListView)    

    findViewById(R.id.expandableListView);
       ExpandableListAdapter mAdapter = new 
       ExpandableListViewAdapterCustom(mGroupsCursor,    
       MyActivity.this,
            R.layout.exp_listview_groupheader,// Your row layout   for a group
            R.layout.exp_listview_childrow, // Your row layout for a child
            new String[] { "Year",
            "TotalSent",
            "TotalReceived" },// Field(s) to use from group cursor       
            new int[] {R.id.txtYearValue,
                       R.id.txtAmountSentValue,
                       R.id.txtAmountReceivedValue },// Widget ids to put group data                        
                       into new String[] { "Year","Month", 
                       "TotalSent", 
                       "TotalReceived" },  // Field(s) to use from child cursors  new  
                        int[] {R.id.txtMonthValue,
                        R.id.txtMonthAmountSentValue,
                        R.id.txtMonthAmountReceivedValue});// Widget ids to put child d   
                        data into
                        elv.setClickable(true);
                        elv.setAdapter(mAdapter); // set the
4

1 回答 1

0

在将近两周没有回答之后,我决定简单地使用一个使用 ArrayLists 的 ExpandableListView 示例并对其进行修改,以便 ArrayLists 由数据库中的数据填充。它不是我想要的,但它有效。我实际上很惊讶,现在网络上有一个使用 ExpandableListview 扩展形式 BaseAdapter 的示例,但使用说 cursorTreeAdapter 或 SimpleCursorAdapter 从 SQlite 读取。以下是我如何做到的,以防将来对某人有所帮助。显示的代码是从 DB 填充 ArrayList 的位

public ArrayList<ExpandListGroup> SetStandardGroups() {

    ArrayList<ExpandListGroup> list = new ArrayList<ExpandListGroup>();
    ArrayList<ExpandListChild> list2 = new ArrayList<ExpandListChild>();
    int intMonthNum;
    ExpandListGroup grp;
    ExpandListChild chld;

    //initialize db code here
    DatabaseRoutines db = new DatabaseRoutines(this);

    //create the Groups retreival cursor;
    Cursor mGroupsCursor = db.fetchGroup();

    //---the database call is done using this code which is in my 
    //---custom db class which implements the sqlhelper methods etc
    //------start of db code snippet------------------------------- 
    //---public Cursor fetchGroup() {
    //---SQLiteDatabase db = this.getReadableDatabase();
    //--- String query = "SELECT DISTINCT MIN(ID) AS id, Year, 
    //--- SUM(SentAmount) AS TotalSent, 
    //--- SUM(ReceivedAmount) AS TotalReceived 
    //--- FROM  Tbl GROUP BY Year ORDER BY Year DESC ";
    //--- return db.rawQuery(query, null);}
    //------end of db code snippet-------------------------------
    mGroupsCursor.moveToFirst();

    //method is depreciated from api14 but i'm targeting Gingerbread (api10) so i need to use it.
    startManagingCursor(mGroupsCursor);
    int intYear;
    int intHeaderCounter = 0;
    int intChildCounter = 0;
    int intChildTotalCount = 0;
    int intHeaderTotalGroupCount = mGroupsCursor.getCount();

    //set the starting Year for the loop, if there is data;
    if (intHeaderTotalGroupCount > 0) {
        //get the first year
        //intYear = mGroupsCursor.getInt(mGroupsCursor.getColumnIndex("Year"));

        for (intHeaderCounter = 0; intHeaderCounter < intHeaderTotalGroupCount; intHeaderCounter++) {

            grp = new ExpandListGroup();
            intYear = mGroupsCursor.getInt(mGroupsCursor.getColumnIndex("Year"));
            grp.setYear(intYear);
            grp.setYearAmountReceived(mGroupsCursor.getDouble(mGroupsCursor.getColumnIndex("TotalReceived")));
            grp.setYearAmountSent(mGroupsCursor.getDouble(mGroupsCursor.getColumnIndex("TotalSent")));
            grp.setTag(mGroupsCursor.getString(mGroupsCursor.getColumnIndex("id")));


            //Prepare counters for inner loop for child items of each
            Cursor mChildCursor = db.fetchChildren(intYear);
            mChildCursor.moveToFirst();
            intChildTotalCount = mChildCursor.getCount();
            //populate child items
            for (intChildCounter = 0; intChildCounter < intChildTotalCount; intChildCounter++) {
                chld = new ExpandListChild();
                intMonthNum = mChildCursor.getInt(mChildCursor.getColumnIndex("Month"));
                chld.setMonthNumber(intMonthNum);
                chld.setTotalReceivedMonth(mChildCursor.getInt(mChildCursor.getColumnIndex("TotalReceived")));
                chld.setTotalSentMonth(mChildCursor.getInt(mChildCursor.getColumnIndex("TotalSent")));
                chld.setTag(mGroupsCursor.getString(mGroupsCursor.getColumnIndex("id")).toString());

                list2.add(chld);
                //grp.setItems(list2);
                //move to next child record;

                mChildCursor.moveToNext();


            }


            grp.setItems(list2);
            list.add(grp);

            list2 = new ArrayList<ExpandListChild>();
            //move to next parent record;
            mGroupsCursor.moveToNext();

        }

    } else {
        log.d( "yourdebugtag_here", "Sorry, No Transactions Found.");
    }

    //db.close();


    return list;
}
于 2013-05-04T12:12:29.397 回答