2

I'll start by saying i've researched this specific solution to death and am aware of the solutions "this has been solved HERE" and HERE, but I am unable to get these working. Other info: I'm using the compatibility library and also actionbarsherlock, which both have been working great, no issues there i dont think.

I haven an SQLite database that i want to fill my entries into expandable list grouped by date. I know that my database is correctly populated and here are the columns I have verified to contain proper data:

ID  (1, 2, 3, 4, etc)

TITLE (some text)

URL  (url as string)

DATE (ddMMyyyy)

where the date column data is like this: 22Dec2012 or 23Dec2012 etc.

There are many entries per date, i want to group those as children with the date as group. I see the database is filled with the correct info. I have a few problems, the main is that my getChildrenCursor never gets called, only the constructor gets called. Second is that per the linked examples, I cannot get the getSherlockActivity context into my adapter (nor does getActivity() work either, just for testing purposes). I'll explain the error with logcat below.

My Fragment

public class MyFragment extends SherlockFragment implements LoaderManager.LoaderCallbacks, OnItemClickListener {    

public static MyFragment newInstance(String symbol) {
    MyFragment f = new MyFragment();

    Bundle args = new Bundle();
    args.putString(Consts.INFO, info);
    f.setArguments(args);

    return f;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View myView = inflater.inflate(R.layout.fragment_layout3, container, false);
    mListView = (ExpandableListView)myView.findViewById(R.id.expandableListView);
    mListView.setOnItemClickListener(this);
    return myView;
}    

@Override
public void onPause() {
    super.onPause();

}    

@Override
public void onResume() {
    super.onResume();

    refresh();
}

public void refresh() {
    Log.v(TAG, "refresh");

    populateExpandableList();

    loader = getLoaderManager().getLoader(-1);
    if (loader != null && !loader.isReset()) {
      getLoaderManager().restartLoader(-1, null, this);
    } else {
      getLoaderManager().initLoader(-1, null, this);
    }

    if (getLoaderManager().getLoader(0x9999) == null) {
        getLoaderManager().initLoader(0x9999, null, this);
        //getLoaderManager().initLoader(-1, null, this);
    }
    else {
        getLoaderManager().restartLoader(0x9999, null, this);
        //getLoaderManager().restartLoader(-1, null, this);
    }
    getLoaderManager().getLoader(0x9999).forceLoad();
    //getLoaderManager().getLoader(1).forceLoad();
}

private void populateExpandableList() {
    // Set up our adapter
    Log.v(TAG, "Endtering setup adapter");
    mDbHelper = new DBHelper(getSherlockActivity(), DBConstants.DATABASE_NAME, null, DBConstants.DATABASE_VERSION);
    mAdapter = new MyExpandableAdapter(getSherlockActivity(), this, 
      R.layout.news_list_group,
      R.layout.news_list_child,
      new String[] { DBConstants.DATE_GROUP_NAME }, // Name for group layouts
      new int[] { R.id.group_title },
      new String[] { DBConstants.TITLE_NAME, DBConstants.URL_NAME }, // Name for child layouts
      new int[] { R.id.item_title, R.id.item_value });

    mListView.setAdapter(mAdapter);
  }    

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // TODO Auto-generated method stub      
}   

@Override
public Loader onCreateLoader(int loaderId, Bundle args) {
    topics = new ArrayList<String>();

    if (loaderId == 0x9999) {
        return new MyLoader(getSherlockActivity(), mHandler, topics.toArray(new String[topics.size()]));
    }
    if (loaderId != -1 && loaderId != 0x9999) {
        Log.v(TAG, "CreateLoader child cursor: loader ID: "+loaderId);
        // child cursor
        mCursorLoader = new SQLiteCursorLoader(getSherlockActivity(), mDbHelper, "SELECT _ID, "+DBConstants.TITLE+", " +DBConstants.URL + ", " + DBConstants.TIME + ", " + DBConstants.STATUS + " FROM "+DBConstants.TABLE+" WHERE "+  DBConstants.KEY_ID+"="+String.valueOf(loaderId)+" ORDER BY "+DBConstants.TIME+" DESC", null);
        return mCursorLoader;
    }       
    else if (loaderId != 0x9999) {
        Log.v(TAG, "CreateLoader group cursor: loader ID: "+loaderId);
        // group cursor
        mCursorLoader = new SQLiteCursorLoader(getSherlockActivity(), mDbHelper, "SELECT date, "+DBConstants.TITLE+", "+DBConstants.URL+" FROM "+DBConstants.TABLE, null);
        return mCursorLoader;
    }

    return null;
}

@Override
public void onLoadFinished(Loader loader, Object result) {
    Log.v(TAG, "onLoadFinished");
    if(result != null /*&& result.length > 0*/) {
        if (loader.getId() == 0x9999) {
            getLoaderManager().restartLoader(-1, null, this);
            getLoaderManager().getLoader(-1).forceLoad();

        }
        if (loader.getId() != -1 && loader.getId() != 0x9999) {
            Log.v(TAG, "LoadFinished second loader: loaderID: "+loader.getId());
            if (!((Cursor) result).isClosed()) {
                Log.v(TAG, "data.getCount() " + ((Cursor) result).getCount());

                HashMap<Integer,Integer> groupMap = mAdapter.getGroupMap();
                try {
                    int groupPos = groupMap.get(loader.getId());
                    Log.v(TAG, "onLoadFinished() for groupPos " + groupPos);
                    mAdapter.setChildrenCursor(groupPos, (Cursor) result);
                } catch (NullPointerException e) {
                    Log.w("DEBUG","Adapter expired, try again on the next query: "
                    + e.getMessage());
                }
            }
            else {
                mAdapter.setGroupCursor((Cursor) result);
            }
        }
        else {

        }
    }
}

@Override
public void onLoaderReset(Loader loader) {
    int id = loader.getId();
    Log.v(TAG, "onLoaderReset() for loader_id " + id);
    if (id != -1) {
        // child cursor
        try {
            mAdapter.setChildrenCursor(id, null);
        } catch (NullPointerException e) {
            Log.w("TAG", "Adapter expired, try again on the next query: "
                    + e.getMessage());
        }
    } else {
      mAdapter.setGroupCursor(null);
    }
}



public class MyExpandableAdapter extends SimpleCursorTreeAdapter  {
    private final String TAG = getClass().getSimpleName().toString();
    private MyFragment mFragment;
    protected HashMap<Integer, Integer> mGroupMap = null;

    public NewsFeedExpandableAdapter(Context context, MyFragment mf, int groupLayout, int childLayout, String[] groupFrom,
            int[] groupTo, String[] childrenFrom, int[] childrenTo) {
        super(context, null, groupLayout, groupFrom, groupTo, childLayout, childrenFrom, childrenTo);       
        mFragment = mf;
        mGroupMap = new HashMap<Integer, Integer>();
        Log.v(TAG, "Adapter constructor");
    }

    @Override
    protected Cursor getChildrenCursor(Cursor groupCursor) {        
        // Given the group, we return a cursor for all the children within that group
        Log.v(TAG, "getChildrenCursor");
        int groupPos = groupCursor.getPosition();
        int groupId = groupCursor.getInt(groupCursor.getColumnIndex(DBConstants.TIME_GROUP_NAME));
        Log.v(TAG, "getChildrenCursor() for groupPos " + groupPos);
        Log.v(TAG, "getChildrenCursor() for groupId " + groupId);

        mGroupMap.put(groupId, groupPos);

        Loader loader = mFragment.getLoaderManager().getLoader(groupId); 
        if ( loader != null && !loader.isReset() ) { 
            mFragment.getLoaderManager().restartLoader(groupId, null, mFragment); 
        } else { 
            mFragment.getLoaderManager().initLoader(groupId, null, mFragment); 
        } 

        return null;    
    }

    //Accessor method
    public HashMap<Integer, Integer> getGroupMap() {
        return mGroupMap;
    }
}

}

Now in my adapter above, you see i'm using :

Loader loader = mFragment.getLoaderManager().getLoader(groupId); 
        if ( loader != null && !loader.isReset() ) { 
            mFragment.getLoaderManager().restartLoader(groupId, null, mFragment); 
        } else { 
            mFragment.getLoaderManager().initLoader(groupId, null, mFragment); 
        } 

rather than this:

Loader loader = getSherlockActivity().getLoaderManager().getLoader(groupId); 
        if ( loader != null && !loader.isReset() ) { 
            getSherlockActivity().getLoaderManager().restartLoader(groupId, null, mFragment); 
        } else { 
            getSherlockActivity().getLoaderManager().initLoader(groupId, null, mFragment); 
        } 

Because when i use getActivity or getSherlockActivity, the code gives an error on the first line above saying:

"Type mismatch, cannot convert from Loader<Object> to Loader"

and on the third and fifth line errors:

"The method restartLoader(int, Bundle, LoaderManager.LoaderCallbacks<D>) in the type LoaderManager is not applicable for the arguments (int, null, MyFragment)"

Does anyone possibly know why i cannot use getActivity here? It's driving me completely bonkers. So i attempt to use mFragment.getLoaderManager instead using the fragment i passed in to the adapter, but i dont know if this is even correct.

I placed breakpoints in the code and it shows that the adapter constructor is called, and that's it, getChildrenCursor never gets called ever. in the fragment breakpoints, it reaches "Create Group Loader id=-1" and thats it. in onLoadFinished only the first "onLoadFinished" tag get's shown in the log. It never makes it into the IF statements within onLoadFinished. When i check that the loadFinished result is not null in logcat, it shows as a cursor object being passed in to onLoadFinished, but something just isnt working right.

prior to trying expandable list, i was using these identical SQLiteCursorLoader's setup, just retrieving all columns, and they worked fine. I am absolutely admitting that maybe both my SELECT statements are not correct. but i've tried a ton of different select queries, and everytime i get same result, nothing. Can anyone PLEASE help me I'm desperate and going bonkers. Thanks.

Community
  • 1
  • 1
RogerPodacter
  • 31
  • 1
  • 6
  • 1
    please dont write stories. tell the exact problem you are facing. Even your question title doesnt tells about the issue. – Sahil Mahajan Mj Dec 24 '12 at 06:20
  • ok I tried to remove some of the text and edit the title, but I thought it was all relevant to the issue. – RogerPodacter Dec 24 '12 at 06:37
  • well i'm a putz, i realized my bottom else statement in onLoadFinished was in the inner loop, should have been moved out one level. now i get groups populated in my expandable list view, but its not correct. i get no children, and repeated group titles for the number of child elements it should have. i need to modify my select statements, but i'm unsure. – RogerPodacter Dec 24 '12 at 07:31

0 Answers0