I am working on an application in which I load and show form with data being loaded over the Internet.It takes some time to load all data so I would like to do it like this. Load static data and show it to the user but without dynamic part from internet and when it shows that form it trigger something like “OnLoadActaivity” (not sure) and then to add views dynamically.
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);
}
}
}
You shouldn’t do long processes on the UI thread. It’s bad practice and isn’t allowed on 3.0+ devices. Use an AsyncTask instead. AsyncTasks allow you to start a process on another thread, and be notified on the UI thread when its complete. You should specifically look at