0

how to update Ui when using Asyntask forloop data ad this line:

final View row2 = createRow2 (school5.getJSONObject(i));
table3.addView(row2);

im updating UI inside forloop but if i use asyntask how do i do this?

   public class fifthscreen extends Activity {

           @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fifthscreen);

            parseJSONData();

}



   public void parseJSONData() {

    SelectMenuAPI = Utils.dishdescription + dish_name;

    clearData();
    URL = SelectMenuAPI;
    URL2 = URL.replace(" ", "%20");

     try {

        HttpClient client = new DefaultHttpClient();
        HttpConnectionParams
                .setConnectionTimeout(client.getParams(), 15000);
        HttpConnectionParams.setSoTimeout(client.getParams(), 15000);
        HttpUriRequest request = new HttpGet(URL2);
        HttpResponse response = client.execute(request);
        InputStream atomInputStream = response.getEntity().getContent();
        BufferedReader in = new BufferedReader(new InputStreamReader(
                atomInputStream));

        String line;
        String str = "";
        while ((line = in.readLine()) != null) {
            str += line;
        }

    final LinearLayout table3 = (LinearLayout) findViewById(R.id.table3);

            JSONArray school5 = json2.getJSONArray("dish_ingredient");

            for (int i=0; i<school5.length(); i++) {

                final View row2 = createRow2 (school5.getJSONObject(i));
                table3.addView(row2);





    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        // IOConnect = 1;
        e.printStackTrace();
    } catch (JSONException e) {
         // TODO Auto-generated catch block
        e.printStackTrace();
    }
}






           public View createRow2(JSONObject item) throws JSONException {

      View row2 = getLayoutInflater().inflate(R.layout.row2, null);
        ((TextView) row2.findViewById(R.id.name)).setText(item.getString("name"));
        ((TextView)  
       row2.findViewById(R.id.subingredients)).setText(item.getString("sub_ingredients"));

    return row2;
}

how to update Ui same like this in postexecute

 public class getDataTask extends AsyncTask<Void, Void, Void>{

        getDataTask(){

        }

        @Override
         protected void onPreExecute() {
          // TODO Auto-generated method stub

        }

        @Override
        protected Void doInBackground(Void... arg0) {
            // TODO Auto-generated method stub
            parseJSONData();
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {

        }
    }

how to show this two lines in onPostExecute

    //       final View row2 = createRow2 (school5.getJSONObject(i));
        //      table3.addView(row2);
Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225
smart guy
  • 9
  • 2
  • 6

3 Answers3

1

Use the Handler object from your MainActivity and post a runnable. To use it from the backgrund you need to make the object a static that you can call outside of your MainActivity or you can create a static instance of the Activity to access it.

Inside the Activity

    private static Handler handler;


    handler = new Handler();

    handler().post(new Runnable() {

        public void run() {
            //ui stuff here :)
            final LinearLayout table3 = (LinearLayout) findViewById(R.id.table3);

            JSONArray school5 = json2.getJSONArray("dish_ingredient");

            for (int i=0; i<school5.length(); i++) {

                final View row2 = createRow2 (school5.getJSONObject(i));
                table3.addView(row2);
        }
    });

    public static Handler getHandler() {
       return handler;
    }

Outside the Activity

MainActivity.getHandler().post(new Runnable() {

        public void run() {
            //ui stuff here :)
        }
    });
JWqvist
  • 717
  • 6
  • 15
  • im confuse so much im already usinf Asyntask and update remaining Ui from PostExecute where i put this code? – smart guy Oct 02 '13 at 07:18
  • dont use the async task just use the handler to update the ui after you for loop – JWqvist Oct 02 '13 at 07:20
  • but i already use asyntask and add parsejson method in backgtroundtask see my thispost http://stackoverflow.com/questions/19131041/how-to-update-ui-from-background-task – smart guy Oct 02 '13 at 07:26
  • i have only feel problem how to shoew row = createRow(school3.getJSONObject(s)); in postexecute – smart guy Oct 02 '13 at 07:26
0

Try to split you code like this. and add the handler if and where you get ui exeption

public class getDataTask extends AsyncTask<Void, Void, Void>{

    getDataTask(){

    }

    @Override
     protected void onPreExecute() {
      // TODO Auto-generated method stub

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        SelectMenuAPI = Utils.dishdescription + dish_name;

clearData();
URL = SelectMenuAPI;
URL2 = URL.replace(" ", "%20");

 try {

    HttpClient client = new DefaultHttpClient();
    HttpConnectionParams
            .setConnectionTimeout(client.getParams(), 15000);
    HttpConnectionParams.setSoTimeout(client.getParams(), 15000);
    HttpUriRequest request = new HttpGet(URL2);
    HttpResponse response = client.execute(request);
    InputStream atomInputStream = response.getEntity().getContent();
    BufferedReader in = new BufferedReader(new InputStreamReader(
            atomInputStream));

    String line;
    String str = "";
    while ((line = in.readLine()) != null) {
        str += line;
    }

final LinearLayout table3 = (LinearLayout) findViewById(R.id.table3);

        JSONArray school5 = json2.getJSONArray("dish_ingredient");

        for (int i=0; i<school5.length(); i++) {

            final View row2 = createRow2 (school5.getJSONObject(i));
            table3.addView(row2);





} catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    // IOConnect = 1;
    e.printStackTrace();
} catch (JSONException e) {
     // TODO Auto-generated catch block
    e.printStackTrace();
}

}

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
public View createRow2(JSONObject item) throws JSONException {

  View row2 = getLayoutInflater().inflate(R.layout.row2, null);
    ((TextView) row2.findViewById(R.id.name)).setText(item.getString("name"));
    ((TextView)  
   row2.findViewById(R.id.subingredients)).setText(item.getString("sub_ingredients"));
    }
}
JWqvist
  • 717
  • 6
  • 15
-1
YourActivity.this.runOnUiThread(new Runnable() {
    public void run() {
        //What you want to do, updating a UI in main thread ect.
    }
});

you could also refer to another post HERE

Community
  • 1
  • 1
Alan Feng
  • 504
  • 4
  • 9