我可以在AsyncTask请求之后操作onCreateView上的数据

时间:2014-09-29 11:14:45

标签: android android-fragments android-asynctask

我正在做一个以这种方式创建片段后启动的AsyncTask。 我的问题是如何在OnCreateView中执行此任务后操作数据! 非常感谢你的帮助。 我在片段创建后获取数据,但我可以显示它们或操纵它们

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ClubsTask clubsTask = new ClubsTask();
    clubsTask.execute((Void) null);
}

这是我的ClubTask课程:

public class ClubsTask extends AsyncTask<Void, Void, String> {

    /*private final String mEmail;
    private final String mPassword;*/

    String url = "index.php/services/GetAllClubs";

    ClubsTask() {

    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    public String doInBackground(Void... params) {
        // TODO: attempt authentication against a network service.

        try {
            // Simulate network access.
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            return "false";
        }

        ArrayList<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>();
        /*nameValuePairs.add(new BasicNameValuePair("email", mEmail));
        nameValuePairs.add(new BasicNameValuePair("password", mPassword));*/

        //Prepare the Post query

        try {
            HttpClient clientHttp = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = clientHttp.execute(httppost);
            HttpEntity entity = response.getEntity();
            InputStream is = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
            StringBuilder sb = new StringBuilder();
            String line = reader.readLine();
            sb.append(line + "\n");
            is.close();

            String result = sb.toString();

            System.out.println(result);

            return result;



        }catch (Exception e){

            return null;
        }

    }

    @Override
    protected void onPostExecute(final String res) {
        //LoginTask = null;
        //showProgress(false);

        try {
            jObj = new JSONArray(res);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        if (!jObj .isNull("id")) {

            //
            try {
                test.setText(j.getString("name"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
            //
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setTitle("Success !");
            builder.setMessage("Results displayed.")
                    .setCancelable(false)
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });
            AlertDialog alert = builder.create();
            alert.show();

        } else {

            //password.setError(getString(R.string.error_incorrect_password));
            //password.requestFocus();
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setTitle("Error !");
            builder.setMessage("Request Error ! Please try later.")
                    .setCancelable(false)
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });
            AlertDialog alert = builder.create();
            alert.show();

        }

    }

    @Override
    protected void onCancelled() {
        clubsTask = null;
        //showProgress(false);
    }
}

2 个答案:

答案 0 :(得分:1)

测试是TextView,对吗? 在这种情况下,您应该在ui线程中更新

test.post(new Runnable() 
{   
  @Override
  public void run() 
  {
        test.setText(j.getString("name"));
  }
});

答案 1 :(得分:1)

是的,你可以使用runonuithread

来做到这一点
runOnUiThread(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            // change your view here
        }
    });