
Untitled
By: a guest on
Jul 29th, 2012 | syntax:
None | size: 1.40 KB | hits: 12 | expires: Never
How to update the text in the UI from the background thread in Android
public class DefaultActivity extends Activity{
TextView textView;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView=(TextView)findViewById(R.id.textId);
new networkFileAccess().execute("background","Progress","result");
}
private class networkFileAccess extends AsyncTask<String,String,String>{
protected String doInBackground(String... background){
return changeText();
}
private String changeText(){
//Code to Access data from the Network.
//Parsing the data.
//Retrieving the boolean Value.
if(booleanistrue){
//Displaying some text on the UI.
publishProgress("someTextOnUI");
//Send request till we get get boolean value as false.
changeText();
}else{
return "success";
}
return "";
}
protected void onProgressUpdate(String... progress){
textView.setText("Wait background work is going on");
}
protected void onPostExecute(String result){
if(result.equals("success")){
//Code to finish the activity.
}
}
}
Thanks&Regards,
Venkat.
runOnUiThread(new Runnable() {
public void run() {
tv.setText("ABC");
}
});
runOnUiThread(new Runnable() {
public void run() {
// change text
}
});