2011-01-30 12 views
0

私は最初のAndroidアプリケーションとして戦艦ゲームで作業します。ViewSwitcherアニメーションが起こる前にGridViewを更新するには?

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

ユーザーがbComputerGridをリフレッシュするために、私はnotifyDataSetChanged(呼び出しuserAttackonPostExecute()節)に、逃した場合。

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

そして、ここuserAttackonPostExecute()セクションで私はAsyncTask computerAttackを開始します。 computerAttackonPreExecute()セクションでは、私はbUserGridにbComputerGridを切り替える

bigSwitcher.showNext(); 
を呼び出すことによって切り替えはなく、bComputerGridは、その前に更新されていないが起こります!

コンピュータは、そのストライキを行い、成功しbUserAdapter.notifyDataSetChanged()を呼び出すことにより、onProgressUpdate()computerAttackセクションで、ユーザーのフィールドを更新します。コンピュータが逃げたとき、アプリケーションはユーザーが再び攻撃を受けるのを待つ(タップ)ようになります。

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

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

    } 

しかし、スイッチングおよびコンピュータショットが起こる前に、なぜ私はbComputerGridを更新することはできませんか?

お願いします。

+0

アダプターの基礎データを変更する前にnotifyDataSetChanged()を呼び出さないでください。 bComputerAdapter.notifyDataSetChanged();でブレークポイントを設定します。何が起こるか見る。 – fiction

+0

あなたの注意、フィクションに感謝します!私は、ブレークポイントを設定し、notifyDataSetChanged()を呼び出す前に基本配列が変更されていることを確認します。 – captaindan

答えて

0

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

+0

あなたの注意をお寄せいただきありがとうございます、オリバー! notifyDataSetChanged()呼び出しの直前のonPostExecute()セクションのアダプタの基礎となるデータを変更します。 updateSeqComputerShips(int1D_to_int2D(mUserAttack.getmUBundle()。getIntArray( "computerShips"))); – captaindan

関連する問題