【问题标题】:how to updateui in loop from postexecute如何从postexecute循环更新ui
【发布时间】:2013-10-02 07:06:14
【问题描述】:

使用 Asyntask forloop 数据时如何更新 Ui 广告这一行:

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

我正在更新 forloop 中的 UI,但如果我使用 asyntask,我该怎么做?

   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;
}

如何在 postexecute 中像这样更新 UI

 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) {

        }
    }

如何在onPostExecute中显示这两行

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

【问题讨论】:

    标签: android


    【解决方案1】:

    使用 MainActivity 中的 Handler 对象并发布一个可运行文件。要在后台使用它,您需要将对象设为静态对象,您可以在 MainActivity 之外调用该对象,或者您可以创建 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;
        }
    

    活动之外

    MainActivity.getHandler().post(new Runnable() {
    
            public void run() {
                //ui stuff here :)
            }
        });
    

    【讨论】:

    • 我很困惑,我已经在使用 Asyntask 并从 PostExecute 更新剩余的 Ui 我把这段代码放在哪里?
    • 不要使用异步任务,只需在 for 循环后使用处理程序更新 ui
    • 但我已经使用 asyntask 并在 backgtroundtask 中添加 parsejson 方法,请参阅我的这篇文章stackoverflow.com/questions/19131041/…
    • 我只觉得问题在于如何设置 row = createRow(school3.getJSONObject(s));在执行后
    【解决方案2】:

    尝试像这样拆分您的代码。并在您获得 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"));
        }
    }
    

    【讨论】:

      【解决方案3】:
      YourActivity.this.runOnUiThread(new Runnable() {
          public void run() {
              //What you want to do, updating a UI in main thread ect.
          }
      });
      

      你也可以参考另一个帖子HERE

      【讨论】:

      • 我将这个 runUiThread 代码放在 mainactivity 内部还是外部?
      • 当您可以访问目标活动(例如主要活动)时,您可以调用此 runOnUiThread。将您的代码发电子邮件给我,我会看看我能为您做什么:alan.kl.feng@gmail。 com
      • 我已发送,请查看我的邮件
      猜你喜欢
      • 2021-04-04
      • 1970-01-01
      • 2011-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多