0

私は最初の Android アプリケーションとして戦艦ゲームに取り組んでいます。

2 つの GridView を保持する ViewSwitcher を使用することにしました。1 つのbUserGridはユーザー フィールド (ユーザー シップをホストする) 用であり、bComputerGridはコンピューター フィールド (ユーザー ショットを表示する) 用です。

GridView bComputerGrid, bUserGrid, mComputerGrid, mUserGrid;
ViewSwitcher bigSwitcher, miniSwitcher;

ユーザー ストライク (コンピュータ フィールドをタップ) アプリケーションは、そのストライキが成功したかどうかを定義するAsyncTask userAttackを実行します。

bComputerGrid.setOnItemClickListener(new OnItemClickListener() {
  public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    runUserAttack(position);
  }
});

ユーザーが失敗した場合、 userAttack のonPostExecute()セクションで、bComputerGridを更新するためにnotifyDataSetChanged ()を呼び出します。

    protected void onPostExecute(String message) {
        ...
        bComputerAdapter.notifyDataSetChanged();
        mComputerAdapter.notifyDataSetChanged();
        firstmateMsg.setText(message);
        mUserBundle = mUserAttack.getmUBundle();
        mUserAttack=null;
        if (isCancelled()) return;
        if (mUserBundle.getBoolean("userMissed")) {
            mComputerAttack = new computerAttack();
            mComputerAttack.execute(mComputerBundle);
        } else {
            if (doesWon(seqComputerShips)) {
                showDialog(0);
            }
        }
    }

しかし、それは起こりません!ユーザーが見逃した場合、次のステートメントはbComputerGridを更新しません。

bComputerAdapter.notifyDataSetChanged();

次に、ここでuserAttack のonPostExecute()セクションでAsyncTask computerAttack開始します。computerAttackのonPreExecute ()セクションで、呼び出してbComputerGridbUserGridに切り替えます

bigSwitcher.showNext();

切り替えが発生しますが、bComputerGridはそれ以前に更新されていません!

コンピューターはストライキを実行し、 bUserAdapter.notifyDataSetChanged()を呼び出してcomputerAttackのonProgressUpdate ()セクションのユーザー フィールドを正常に更新します。コンピューターが逃した場合、アプリケーションは再びユーザーのストライク (タップ) を待つようになります。

    protected void onProgressUpdate(String... messages) {
        ...
        mUserAdapter.notifyDataSetChanged();
        bUserAdapter.notifyDataSetChanged();

    }
    protected void onPostExecute(Boolean result) {
        mComputerBundle = mComputerAttack.getmCBundle();
        mComputerAttack=null;
        if (field_switch) {
            switchViews();
        }

    }

しかし、その切り替えとコンピュータ ショットが発生する前にbComputerGridを更新できないのはなぜですか?

手伝っていただけませんか?

4

1 に答える 1

0

アダプターをどこで更新しますか? アダプターの基になるデータを変更しますか、それともアダプター リストを変更しますか? アダプターも UI の一部であるため、アダプター自体の変更は GUI スレッドでのみ行う必要があります。サービススレッドでアダプターの内容を変更したときに、他のプロジェクトでそう言っているUI例外(表示中)がいくつかありました。最終的には、基になるデータの変更を反映するためにアダプター リストを再作成する必要がありますか?

于 2011-01-30T15:37:50.903 に答える