如何通知更新数据库的游标适配器

时间:2013-06-16 20:46:21

标签: android sqlite gridview simplecursoradapter

我的gridview活动目前的方式在我的onCreate方法中我执行异步任务,搜索手机上的所有视频,然后使用do while循环将id和文件路径存储在SQLite数据库中。然后我调用一个使用后台线程的游标适配器来使用文件路径来创建缩略图并在gridview中显示它们。但是,我遇到的问题是,当我第一次启动活动时,gridview中没有显示任何内容。但是,当我再打开它时,一切都会显示出来。所以我的问题是,当活动首次启动时,游标中没有任何内容,因此无法显示任何内容。

我的问题是,当在后台线程中输入新数据时,我将如何更新游标适配器?如何触发游标适配器以使用刚刚在后台线程中输入的数据?我的代码发布在下面(抱歉它有点草率)。

的OnCreate

public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.preview);
GridView gridview = (GridView) this.findViewById(R.id.gridview);

cursor = getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, mediaColumns, null, null, null);
columnindexid = cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID);
columnindexdata = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);

entry = new GridviewData(this);
entry.open();

DataEntry putitin = new DataEntry(entry);
putitin.execute();

//the cursor used in the cursor adapater
Cursor curs = entry.adapterCursor();
videoidindex = entry.Indexfinder(curs);
videopathindex = entry.Indexfinder2(curs);

config = new ImageLoaderConfiguration.Builder(this)
.imageDownloader(new BaseImageDownloader(this))
.build();

ImageLoader.getInstance().init(config);
Log.i(TAG, "Before set adapater");
gridview.setAdapter(new VideoAdapter(this, curs, flags));
 }

将数据放入数据库的Asynctask

private class DataEntry extends AsyncTask<Void, Integer, GridviewData>{
Cursor cursor;
GridviewData dataentry;
DataEntry(GridviewData gridviewdata){
    this.dataentry = gridviewdata;
    this.cursor = getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
            mediaColumns, null, null, null);

         columnindexid = cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID);
         columnindexdata = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
}

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

    cursor.moveToFirst();
    do {

        String videoid = cursor.getString(columnindexid);
        String videopath = cursor.getString(columnindexdata);

        int result = dataentry.findVideoID(videoid);
        if (result == 1){

            //this is where data is put into the database
            dataentry.addVideoinfo(videoid, videopath);

        }

        if (result == 0){
            Log.i(TAG, "Cursor wasn't processed, no getcount");
        }
        if(result == 2){
            Log.i(TAG, "The data is already there");
        }

    } while (cursor.moveToNext());
    Log.i(TAG, "After dowhile loop");
    cursor.close();

    return dataentry;   
}   
    }

光标适配器

class VideoAdapter extends CursorAdapter {
Context context; 
public VideoAdapter(Context context, Cursor c, int flags) {
    super(context, c, flags);
    this.context = context;
    }

@Override
public void bindView(View view, Context context, Cursor cursor) {
    ViewHolder holder = (ViewHolder) view.getTag();
    String fileid = cursor.getString(videoidindex);
    String filepath = cursor.getString(videopathindex);
    BitmapDownloader bitdl = new BitmapDownloader(fileid, filepath,    holder.imageview);
    bitdl.execute();
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View eachgrid = inflater.inflate(R.layout.eachgrid, parent, false);
    ViewHolder holder = new ViewHolder();
    holder.imageview = (ImageView) eachgrid
            .findViewById(R.id.Imageview1);
    eachgrid.setTag(holder);
    return eachgrid;        
}

1 个答案:

答案 0 :(得分:2)

在后台线程完成后,在适配器上调用notifyDataSetChanged

  

通知附加的观察者基础数据已经存在   已更改,任何反映数据集的视图都应自行刷新。