android ExpandableListView and loader

135 views Asked by At

I'm trying to use a ExpandableListView with loader but i have some problem. My objectif is to make a first query on my database and i group it by month, then for each month made a query for all depense by month. I'm using a custom content provider for access my database.I haven't probleme to initiate my groupView but i'm in trouble for my childView.I'don't know how to make my query... my fisrt query: SELECT month,amount FROM OutGoUrlData GROUP BY month ORDER BY month ; and my second for the childView: SELECT amount FROM OutGoUrlData WHERE month = XXX AND nameCarId = 1; where XXX is depend of my parent group

MyActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.out_go_layout);
    expandableListView = (ExpandableListView)this.findViewById(R.id.expandableListView);
    Log.d("treeadaptertag", "OutGoActivity thread : " + Thread.currentThread());
    debugId = (TextView)this.findViewById(R.id.infoIdDebug_out_go);
  //  initSpinner();
    Intent i = this.getIntent();
     s = i.getLongExtra("id", -1);//From main activity

    String[] groupFrom={DataCarContract.OUTGO_COLUMN_MONTH};
    int[] groupTo={android.R.id.text1};
    String[] childFrom={DataCarContract.OUTGO_COLUMN_DATE,DataCarContract.OUTGO_COLUMN_AMOUNT};
    int[] childTo={R.id.out_go_textview,R.id.amount_textView};

    outGoSimpleCursorTreeAdapter = new OutGoSimpleCursorTreeAdapter(this,
            null,
            android.R.layout.simple_expandable_list_item_1,
            R.layout.spinner_item_layout,
            groupFrom,
            groupTo,
            R.layout.spinner_item_layout,
            childFrom,
            childTo);

    expandableListView.setAdapter(outGoSimpleCursorTreeAdapter);
    getLoaderManager().initLoader(-1, null, this);

}

  @Override
public Loader<Cursor> onCreateLoader(int loaderID, Bundle args) {
    Log.d(OUTGO_TAG, "onCreateLoader OutGoActivity");

    String[] projectionChild = {DataCarContract._ID, DataCarContract.OUTGO_COLUMN_AMOUNT, DataCarContract.OUTGO_COLUMN_PRICE,DataCarContract.OUTGO_COLUMN_DATE};
    String selectionChild = DataCarContract.OUTGO_NAME_CAR_ID + "=? AND  " + DataCarContract.OUTGO_COLUMN_MONTH+" =?";
    String[] selectionChildArgs = {String.valueOf(s), String.valueOf(XXX)};//need value of the group parent month???


    String[] projectionBill = {DataCarContract._ID, DataCarContract.BILL_TYPE};

    if(loaderID ==-2){
        return new CursorLoader(this, DataCarContract.BILLURL_TABLE_CONTENTURI, projectionBill, null, null, null);
    }
    else if(loaderID != -1){
        //Child cursor id
        return new CursorLoader(this,DataCarContract.OUTGOURL_TABLE_CONTENTURI,projectionChild,selectionChild ,selectionChildArgs,null);
    }else{
        //Parent cursor id
        String[] projectionGroup = {DataCarContract._ID, DataCarContract.OUTGO_COLUMN_MONTH};
        String selectionParent = DataCarContract.OUTGO_NAME_CAR_ID + "=? ";
        String[] selectionArgsParents = {String.valueOf(s)};
        CursorLoader cl = new CursorLoader(this,DataCarContract.OUTGOURL_TABLE_CONTENTURI,projectionGroup,selectionParent+" GROUP BY (" + DataCarContract.OUTGO_COLUMN_MONTH+ ")",selectionArgsParents ,null);
         return  cl;
    }

Here is my methode for get the child view from my adapter(I extend SimpleCursorTreeAdapter)

@Override
protected Cursor getChildrenCursor(Cursor groupCursor) {

    Log.d(SIMPLE_CURSOR_TREE_TAG, "getChildrenCursor");

    int id = groupCursor.getInt(groupCursor.getColumnIndex(DataCarContract.COLUMN_NAME_ROWID));
    int pos =  groupCursor.getPosition();
    //String month = groupCursor.getString(groupCursor.getColumnIndex(DataCarContract.OUTGO_COLUMN_MONTH));
    hashMap.put(id, pos);
    sparseArray.append(id,pos);


    Loader<Cursor> childCursorLoader = ctxOut.getLoaderManager().getLoader(id);

    if (childCursorLoader!=null &&!childCursorLoader.isReset() ){
        ctxOut.getLoaderManager().restartLoader(id,null,ctxOut);
    }else{
        ctxOut.getLoaderManager().initLoader(id,null,ctxOut);
    }
    return null;
}

My problem is that i need data from my cursor to made my query but i don't know how to "pass" my data from my adapter to my loader. Here is a picture that what i need. enter image description here May be a better solution....? Can you help me please.

0

There are 0 answers