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

私のonclickメソッド

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

問題:

進行状況ダイアログは完全に表示されており、データベース内の位置も変更されています。しかし、グリッドビューは更新されていません。つまり、すべての作業が完了すると、進行状況ダイアログが消え、画面が空白になります。

私が間違っているところを助けてください。

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 に答える