Android SQLite:CursorAdapter卡在构造函数中,可能是错误的Cursor

时间:2015-04-12 21:45:48

标签: android sqlite adapter android-cursoradapter

在我的应用程序中,有一个数据库,它还有一个主/详细流类型列表界面。该列表有一个CursorAdapter。当我尝试使用Cursor启动CursorAdapter时,它会卡在构造函数中。

实施如下:

public class PlaceAdapter extends CursorAdapter {

public PlaceAdapter(Context context, Cursor c) {
    super(context, c, false);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    final LayoutInflater inflater = LayoutInflater.from(context);
    View row = inflater.inflate(R.layout.placerow, null);
    bindView(row, context, cursor);
    return row;
}

// UI elements
@Override
public void bindView(View view, Context context, Cursor cursor) {
    // Place from Cursor
    Place place = PlaceDbLoader.getPlaceByCursor(cursor);

    // UI elements...
    ImageView imageViewIcon = ...

}


@Override
public Place getItem(int position) {
    getCursor().moveToPosition(position);

    return PlaceDbLoader.getPlaceByCursor(getCursor());
}

}

我正在尝试使用该适配器来处理我的列表项。在ListFragment中,我有一个AsyncTask来获取所有数据并返回可敬的Cursor:

//Async task for fetching data
private class GetAllTask extends AsyncTask<Void, Void, Cursor> {

    private static final String TAG = "GetAllTask";

    @Override
    protected Cursor doInBackground(Void... params) {
        try {
            Log.d(TAG, "Doinbg0");

            //IMPORTANT
            Cursor result = dbLoader.fetchAll();

            if (!isCancelled()) {
                Log.d(TAG, "Doinbg1");
                if(result==null)Log.d(TAG, "DoinbgNULL");
                return result;
            } else {
                Log.d(TAG, "Cancelled, closing cursor");
                if (result != null) {
                    result.close();
                }
                Log.d(TAG, "Doinbg2");
                return null;
            }
       } catch (Exception e) {
            Log.d(TAG, "Doinbg3");
            return null;
        }
    }

    @Override
    protected void onPostExecute(Cursor result) {
        super.onPostExecute(result);

        Log.d(TAG, "Fetch completed, displaying cursor results!");
        try {
            if (adapter == null) {
                Log.d(TAG, "onPostExec 0");

                if(result==null) Log.d(TAG, "onPostExec NULL");

                //IMPORTANT
                adapter = new PlaceAdapter(getActivity()
                        .getApplicationContext(), result);

                Log.d(TAG, "onPostExec 1");

                setListAdapter(adapter);

                Log.d(TAG, "onPostExec 2");
            } else {
                Log.d(TAG, "onPostExec 3");
                adapter.changeCursor(result);
                Log.d(TAG, "onPostExec 4");
            }


            getAllTask = null;
            Log.d(TAG, "onPostExec done");
        } catch (Exception e) {
        }
    }

}

问题是它被卡在了

adapter = new PlaceAdapter(getActivity()
                    .getApplicationContext(), result);

根据logcat,Log消息如下:

GetAllTask﹕ Doinbg0
GetAllTask﹕ Doinbg1
GetAllTask﹕ Fetch completed, displaying cursor results!
GetAllTask﹕ onPostExec 0

我怀疑result或数据库本身出了问题。

以下是dbLoader.fetchAll();实施:

public Cursor fetchAll(){
    // cursor for all records (where = null)
    return mDb.query(
            DbConstants.Place.DATABASE_TABLE,
            new String[]{
                    DbConstants.Place.KEY_TITLE,
                    DbConstants.Place.KEY_DESCRIPTION,
                    DbConstants.Place.KEY_LAT,
                    DbConstants.Place.KEY_LNG,
                    DbConstants.Place.KEY_TYPE,
                    DbConstants.Place.KEY_RADIUS,
                    DbConstants.Place.KEY_ENABLED
            }, null, null, null, null, DbConstants.Place.KEY_TITLE);
}

以下是创建表格的脚本:

public static final String DATABASE_CREATE =
            "create table if not exists "+DATABASE_TABLE+" ("
                    + KEY_ROWID +" integer primary key autoincrement, "
                    + KEY_TITLE + " text not null, "
                    + KEY_DESCRIPTION +" text, "
                    + KEY_LAT + " real not null, "
                    + KEY_LNG +" real not null, "
                    + KEY_TYPE +" text, "
                    + KEY_RADIUS +" real not null, "
                    + KEY_ENABLED +" text"
                    + ");";

如果有人能指出我在这一方面的正确方向,我将不胜感激,我完全不知道这里出了什么问题。


解决方案:

我发现了问题:我忘记在DbConstants.Place.KEY_TITLE,查询中加入fetchAll()

1 个答案:

答案 0 :(得分:0)

好的..我做了一点挖掘,我找到了原因 - 你在newView()方法中调用bindView()。您不应该进行此调用,因为Android会自动调用bindView()来扩充视图并附加数据。

你正在调用它永远不会返回的方法,因此你只是看到无尽的加载圈。所以现在你的新方法应该是这样的:

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    LayoutInflater inflater = LayoutInflater.from(context);
    View row = inflater.inflate(R.layout.placerow, null);
    return row;
}