0

I’m doing an AsyncTask wich is launched after the creation of the fragment this way. My question is how i can manipulate data after executing this Task in OnCreateView! Thanks a lot for your help. I'm getting the data after fragment creation but can i display them or manipulate them

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

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

This my ClubTask Class :

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);
    }
}
Meenal
  • 2,879
  • 5
  • 19
  • 43
K.Ly
  • 75
  • 3
  • 11

2 Answers2

1

test is TextView, right? In that case You should update that in ui thread

test.post(new Runnable() 
{   
  @Override
  public void run() 
  {
        test.setText(j.getString("name"));
  }
});
Gooziec
  • 2,736
  • 1
  • 13
  • 18
1

yes you can do this using runonui thread

runOnUiThread(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            // change your view here
        }
    });
Meenal
  • 2,879
  • 5
  • 19
  • 43
  • read this link http://stackoverflow.com/questions/3652560/what-is-the-android-uithread-ui-thread – Meenal Sep 29 '14 at 13:16