0

在我的 Android 应用程序中,我有一个将图像显示为项目的网格。当我单击一个按钮时,项目应该改变它们的位置。我正在通过光标适配器填充此网格。以前它工作正常,但需要一些时间来更改图像的位置并再次刷新网格。因此,为此我实现了一个进度对话框,以便用户可以了解正在发生的事情。

到目前为止,这是我的代码。

我的处理程序

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();

我的点击方法

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();

问题:

进度对话框显示完美,数据库中的位置也发生了变化。但gridview不刷新。我的意思是在完成所有工作后,进度对话框消失并且屏幕变为空白。

请帮助我在哪里做错了?

4

1 回答 1

0

我认为问题在于您在处理程序上分配了一个新适配器,但没有在主线程上调用 NotifyDataSetChanged(),这可能就是为什么不更新网格的原因。

另外,为什么不使用 AsyncTasks?

public class LoadAsyncTask extends AsyncTask<Void, Void, Boolean> {
    Context context;

    public LoadAsyncTask(Context context) {
        this.context = context;         
    }

    protected void onPreExecute() {
    }

    protected Boolean doInBackground(Void... v) {
        DBAdapter adapter = new DBAdapter(context);
        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();

        DBAdapter adapter = new DBAdapter(context);
        adapter.open();
        Cursor cursor = adapter.getAllImages();
        adapter.close();
        startManagingCursor(cursor);
        cursorAdapter.changeCursor(cursor); //My cursor adapter
        gridView.setAdapter(cursorAdapter);
        cursor.close();                
        return true;
    }

    protected void onPostExecute(Boolean success) {
        if (success) {
            //This will be executed on the main activity thread
            cursorAdapter.notifyDataSetChanged();
        } else {
            showError();
        }
    }

}
于 2012-10-13T20:27:41.410 回答