显示表格后添加视图

时间:2011-07-06 17:04:26

标签: java android

我正在开发一个应用程序,我在其中加载并显示表单,数据通过Internet加载。加载所有数据需要一些时间,所以我想这样做。加载静态数据并将其显示给用户,但没有来自互联网的动态部分,当它显示该表单时,它会触发类似“OnLoadActaivity”(不确定)的内容,然后动态添加视图。

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 :(得分:0)

您不应该在UI线程上执行长进程。这是不好的做法,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