为什么ExpandableListView的SimpleCursorTreeAdapter中的getChildrenCursor不起作用?

时间:2015-05-18 12:52:15

标签: android database cursor expandablelistview simplecursortreeadapter

我是Android新手。我有一个SQL数据库,想要在ExpandableListView中加载数据并使用SimpleCursorTreeAdapter。我已经测试 fetchGroup fetchChildren 他们工作得很好。但是在logcat中, getChildrenCursor 无法正常工作。无法解决很多天,谁能帮我看看?非常感谢你。

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

MyDatabaseHelper中的fetchGroup和fetchChildren

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

我的创建表

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 个答案:

没有答案