Why the getChildrenCursor in SimpleCursorTreeAdapter of ExpandableListView doesn't work?

279 Views Asked by At

I am new in Android.I have a SQL database and want to load data from it in ExpandableListView and use the SimpleCursorTreeAdapter.I have been test fetchGroup and fetchChildren and them work good.But in logcat, the getChildrenCursor doesn't work.Failed to resolve many days, who can help me to see? Thank you very much.

LogCat

05-18 20:32:24.500  30986-30986/com.hl.gnnucontacts D/gunncontacts﹕ fillData start
05-18 20:32:24.510  30986-30986/com.hl.gnnucontacts D/gunncontacts﹕ is in fetchGroup
05-18 20:32:24.520  30986-30986/com.hl.gnnucontacts D/gunncontacts﹕ the size of mGroupsCursor is : 20
05-18 20:32:24.520  30986-30986/com.hl.gnnucontacts D/gunncontacts﹕ com.hl.gnnucontacts.MainActivity$MyListAdapter@27d1f690
05-18 20:32:24.530  30986-30986/com.hl.gnnucontacts D/gunncontacts﹕ the size of listView is : 20
05-18 20:32:24.530  30986-30986/com.hl.gnnucontacts D/gunncontacts﹕ fillData end

fetchGroup and fetchChildren in MyDatabaseHelper

 public Cursor fetchGroup(SQLiteDatabase mDb) {

        Cursor cursor = mDb.query("Person", new String[]{"college"}, null, null,
                    null, null, null);
        return cursor;
    }



 public Cursor fetchChildren(SQLiteDatabase mDb, String college) {

        Cursor cursor = mDb.query("Person", new String[]{"name"}, "college = ?",
                new String[]{college}, null, null, null);

        Log.d(MainActivity.TAG, "is in fetchChildren");
        return cursor;
    }

MainActivity

    public class MainActivity extends Activity{

    public static final String TAG = "gunncontacts";
    public MyDatabaseHelper dbHelper;
    public SQLiteDatabase db;
    public Cursor mGroupsCursor;

    private LoadFragment loadFragment = new LoadFragment();
    private ExpandableListView listView;
    private MyListAdapter mAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dbHelper = new MyDatabaseHelper(this, "PersonTable.db", null, 1);
        db = dbHelper.getWritableDatabase();
        new MyTask().execute();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mGroupsCursor.close();
        db.close();
    }

    public class MyTask extends AsyncTask<Void, Void, Void>{

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            getFragmentManager().beginTransaction()
                    .add(R.id.container, loadFragment)
                    .commit();
        }

        @Override
        protected Void doInBackground(Void... params) {

            SystemClock.sleep(1000);

            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            Toast.makeText(getApplicationContext(), "Parse Done",
                    Toast.LENGTH_SHORT).show();

            getFragmentManager().beginTransaction()
                    .remove(loadFragment)
                    .commit();

            fillData();

            mGroupsCursor.close();
            db.close();
        }

    }

    public void fillData() {
        Log.d(TAG, "fillData start");
        mGroupsCursor = dbHelper.fetchGroup(db);
        startManagingCursor(mGroupsCursor);
        mGroupsCursor.moveToFirst();
        Log.d(TAG, "the size of mGroupsCursor is : " + mGroupsCursor.getCount() + "");

        listView = (ExpandableListView) findViewById(android.R.id.list);

        mAdapter = new MyListAdapter(this, mGroupsCursor,
                android.R.layout.simple_expandable_list_item_1,
                new String[] {"college"}, new int[] { android.R.id.text1 },
                android.R.layout.simple_expandable_list_item_1,
                new String[] {"name"}, new int[] { android.R.id.text1});
        Log.d(TAG, mAdapter.toString());

        listView.setAdapter(mAdapter);
        Log.d(TAG, "the size of listView is : " + listView.getCount() + "");
        listView.setVisibility(View.VISIBLE);

        Log.d(TAG, "fillData end");
    }

    public class MyListAdapter extends SimpleCursorTreeAdapter {

        public MyListAdapter(Context context, Cursor cursor,
                             int groupLayout, String[] groupFrom, int[] groupTo,
                             int childLayout, String[] childFrom, int[] childTo){
            super(context, cursor,
                    groupLayout, groupFrom, groupTo,
                    childLayout, childFrom, childTo);
        }

        @Override
        protected Cursor getChildrenCursor(Cursor groupCursor) {
            Log.d(TAG, "getChildrenCursor start");
            Cursor childCursor = dbHelper.fetchChildren(db,
                    groupCursor.getString(groupCursor.getColumnIndex("college")));
            Log.d(TAG, "the size of childCursor is : " + childCursor.getCount()+"");
            startManagingCursor(childCursor);
            childCursor.moveToFirst();
            Log.d(TAG, "getChildrenCursor end");
            return childCursor;
        }
    }
}

My Create table

public static final String CREATE_PERSON = "create table if not exists Person("
        + "id integer primary key autoincrement, "
        + "name text not null, "
        + "college text not null, "
        + "tel text not null)";
0

There are 0 best solutions below