2011-07-06 7 views
0

私はデータをインターネットにロードしてフォームをロードして表示するアプリケーションで作業しています。このようにすべてのデータをロードするには時間がかかります。静的なデータを読み込んでユーザーに表示しますが、インターネットからの動的な部分は表示されません。そのフォームを表示すると、「OnLoadActaivity」(わかりません)のようなものがトリガーされ、ビューが動的に追加されます。あなたはUIに長いプロセスを行うべきではありませんフォームを表示した後にビューを追加する

gamestatistics.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center|bottom" 
    android:orientation="vertical"> 
    <TextView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/topPlayers" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal" 
     android:text="Top players for this game:" 
     android:textSize="15sp" 
     android:textColor="#FFFFFF" 
     android:textStyle="bold" 
     android:paddingBottom="10px"/> 
    <HorizontalScrollView android:id="@+id/statisticsScroll" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/playerContainer" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:paddingLeft="5px" 
      android:paddingRight="5px"> 
     </LinearLayout> 
    </HorizontalScrollView> 
</LinearLayout> 

GameStatistics.java

public class GameStatistics extends Actavity{ 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.gamestatistics); 

    Bundle game = getIntent().getExtras(); 
    String gameName = game.getString("gameName"); 
    List playersData = game.getStringArrayList("playersData"); 

    TextView gameNameHeader = (TextView)findViewById(R.id.statisticsGameName); 
    gameNameHeader.setText(gameName);  
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    Bundle game = getIntent().getExtras(); 
    List playersData = game.getStringArrayList("playersData"); 

    LinearLayout playerContainer = (LinearLayout) findViewById(R.id.playerContainer); 

    LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    for (int i=0; i<playersData.size(); i++) 
    { 
     View v = vi.inflate(R.layout.profiletemplate, null); 

     TextView playerName = (TextView) v.findViewById(R.id.playerName); 
     playerName.setText(((String[])playersData.get(i))[0]); 

     TextView playerPosition = (TextView) v.findViewById(R.id.playerScore); 
     playerPosition.setText(((String[])playersData.get(i))[1]); 

     ImageView playerImage = (ImageView) v.findViewById(R.id.playerImage); 

     try 
     { 
      URL url = new URL(((String[])playersData.get(i))[2]); 
      InputStream content = (InputStream)url.getContent(); 
      Drawable d = Drawable.createFromStream(content,"src"); 
      playerImage.setImageDrawable(d); 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
     }    

     playerContainer.addView(v);   
    } 
} 

}

+1

これまで何を試しましたか?これは「ポストアンドリーチコード」サイトではありません。 –

+0

返信ありがとうございます。 これはメソッドの魔法使いが私の問題(onResume)を解決すると思っていましたが、インターネットからデータをロードするのを待ってからフォームを表示するので、ロードしてフォームを表示して後で更新できるメソッドがありますか? – Kec

答えて

0

糸。悪い習慣であり、3.0以上のデバイスでは許可されていません。代わりにAsyncTaskを使用してください。 AsyncTasksを使用すると、別のスレッドでプロセスを開始し、その完了時にUIスレッドで通知することができます。あなたは特に見てください

onPreExecute() //Done on the UI Thread before the execution begins 
doInBackground() //This is the execution thread 
onPostExecute() //Done on the UI Thread after the executing is finished 
+0

ありがとう私はこの方法で試してみます – Kec

関連する問題