Reputation: 839
The way my gridview activity currently stands in that on my onCreate method I kick of a async task that searches for all the videos on my phone and then stores the id and filepath in a SQLite database using a do while loop. I then call a cursor adapter that uses a background thread to use the file path to create thumbnails and display them in the gridview. However, I'm running into the problem that when I first start up the activity nothing displays in the gridview. However, when I open it again everything will display. So my problem is that when the activity first starts the cursor has nothing in it so nothing can be displayed.
My question is how would I go about updating the cursor adapter as the new data is being entered in the background thread? How could I trigger the cursor adapter to use the data that was just entered in the background thread? My code is posted below (sorry its a little sloppy).
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 adapter
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 adapter");
gridview.setAdapter(new VideoAdapter(this, curs, flags));
}
Asynctask that puts data into database
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;
}
}
Cursor adapter
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;
}
Upvotes: 1
Views: 1111
Reputation: 16450
Call notifyDataSetChanged on your adapter after your background thread is done.
Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.
Upvotes: 2