【问题标题】:How to load EditText fields with data fetched from server如何使用从服务器获取的数据加载 EditText 字段
【发布时间】:2013-12-14 18:38:59
【问题描述】:

我有一个活动,在显示 Text/EditText 字段之前,我想调用服务器以获取详细信息,然后根据从服务器返回的数据对字段进行setText

以下是我正在做的事情,但这些字段似乎没有从服务器获取数据。我想是因为我打电话给AsyncTask,它在后台运行,同时这些字段显示给用户。

问题

android 是如何处理这个问题的?我应该使用什么模式?

MainActivity.java 调用此活动,如下所示:

Intent act = new Intent(getApplicationContext(), MySecondActivity.class);
create.putExtra("theId", "138");
startActivity(create);

MySecondActivity.java 中,我执行以下操作:

public class MySecondActivity extends SherlockActivity {

private EditText fieldOne;
private EditText fieldTwo;
private MyObj obj = new MyObj();
private int id;

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.shared_activity);
    fieldOne = (EditText)findViewById(R.id.field_one);
    fieldTwo = (EditText)findViewById(R.id.field_two);

    id = Integer.parseInt(getIntent().getStringExtra("theId"));
    new FetchDetail().execute();

    //If I put the below two lines inside the AsyncTask then I get an error:
    //"Only the original thread that created a view hierarchy can touch its views."
    fieldOne.setText(obj.getOne()); //
    fieldTwo.setText(obj.getTwo()); //

}

class FetchDetail extends AsyncTask<String, String, String> {
    @Override
    protected String doInBackground(String... strings) {
    final RestAdapter restAdapter = new     
    RestAdapter.Builder().setServer("http://10.0.2.2:8080").build();
    final MyTaskService apiManager = restAdapter.create(MyTaskService.class);
    final MyObj obj = apiManager.getDetails(id);
    return null;
    }
}
}

【问题讨论】:

    标签: android android-intent android-asynctask


    【解决方案1】:
    If I put the below two lines inside the AsyncTask then I get an error
    

    这些在onPostExcute

    fieldOne.setText(obj.getOne()); 
    fieldTwo.setText(obj.getTwo());
    

    doInbackground 中进行后台计算。在doInbackground 中返回结果。 doInbackground 计算的结果是一个婴儿车到onPostExecute

    所以你可以在onPostExecute 中更新ui,这是在ui 线程上调用的

    例子:

    protected String doInBackground(String... params)
    {
     // background computation
    return "hello"; // return string
    }
    @Override
    protected void onPostExecute(String result) // string 
    {
         super.onPostExecute(result);
         fieldOne.setText(result);   // hello is set to field One
    }
    

    有关更多信息,请阅读文档中 The4Steps 下的主题

    http://developer.android.com/reference/android/os/AsyncTask.html

    【讨论】:

      【解决方案2】:

      AsyncTask 有 3 个方法可以覆盖:

      1:onPreExecute

      在 UI 线程上执行。因此,在此处调用服务之前,请在 UI 上执行您想要执行的操作(例如:显示进度对话框)。

      2:做背景

      在后台执行,以便执行长时间运行的任务,例如从服务器获取数据。

      3:onPostExecute

      在 UI 线程上执行并在 doInBackground 完成后被调用,您可以在此处处理结果并更新 UI

      例如:

      public class RestServiceTask extends AsyncTask<String, Void, String> {
      
              @Override
              protected void onPreExecute() {
                  super.onPreExecute();
              }
      
              @Override
              protected String doInBackground(String... params) {
      
              }
      
              @Override
              protected void onPostExecute(String result) {
      
              }
      }
      

      【讨论】:

        猜你喜欢
        • 2011-07-16
        • 2013-01-12
        • 2011-05-28
        • 2011-01-21
        • 2019-12-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多