In my Android app I have a grid showing images as items. When I click a button the items should be change their positions. I am populating this grid through a cursor adapter. Previously it was working fine but was taking some time to change the positions of the images and refresh the grid once again. So for that i implemented a progress dialog, so that user could understand that something is going on.
Here is my code so far.
My Handler
public void handleMessage(Message msg) {
switch (msg.what) {
case PROGRESS_DIALOG_HANDLER_ID:
progressDialog.dismiss(); //ProgressDialog
DBAdapter adapter = new DBAdapter(SplittedImageActivity.this);
adapter.open();
Cursor cursor = adapter.getAllImages();
adapter.close();
startManagingCursor(cursor);
cursorAdapter.changeCursor(cursor); //My cursor adapter
gridView.setAdapter(cursorAdapter);
cursor.close();
My onclick method
progressDialog = ProgressDialog.show(SplittedImageActivity.this, "", "Please wait...");
new Thread(){
public void run() {
Random random = new Random();
DBAdapter adapter = new DBAdapter(getApplicationContext());
adapter.open();
int childs = gridView.getChildCount(), oldPosition, newPotision;
newPotision = childs-1;
for(int i=0 ; i<childs ; i++){
oldPosition = random.nextInt(m_cGridView.getChildCount());
adapter.updatePosition(oldPosition, newPotision); //updates the position of the images in database
newPotision = oldPosition;
}
adapter.close();
handler.sendEmptyMessage(PROGRESS_DIALOG_HANDLER_ID);
};
}.start();
Problems:
The progress dialog is showing eprfectly, the positions are also changed in the database. but the gridview is not refreshing. I mean after all works done the progress dialog disappears and the screen becomes blank.
Please help me where i am doing wrong?
I think the problem is that you are assigning a new adapter on the handler, but not calling to NotifyDataSetChanged() on the main thread, that’s maybe why is not updating the grid.
Also, why not use AsyncTasks?