Loading a new fragment from an OnClick set in asyncTask

Search This thread

searayman

Member
Jun 19, 2013
12
1
I have a fragment, which contains a button that when pressed loads a new fragment. The new fragment runs an async task to populate a listview with data.

I am running into trouble, trying to load a new fragment from the onClick. The problem is I can not get the getFragmentManager();


My async task looks like this:


Code:
public class GetStyleStatisticsJSON extends AsyncTask<String, Void, String> {

    Context c;
    private ProgressDialog Dialog;
    android.support.v4.app.Fragment Fragment_one;

    public GetStyleStatisticsJSON(Context context)
    {
        c = context;
        Dialog = new ProgressDialog(c);
    }

    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub
        return readJSONFeed(arg0[0]);
    }

    protected void onPreExecute() {
        Dialog.setMessage("Analyzing Statistics");

        Dialog.setTitle("Loading");
        Dialog.setCancelable(false);
        Dialog.show();
    }

    protected void onPostExecute(String result){
        //decode json here
        try{
            JSONArray jsonArray = new JSONArray(result);


            //acces listview
            ListView lv = (ListView) ((Activity) c).findViewById(R.id.yourStyleStatistics);

            //make array list for beer
            final List<StyleInfo> tasteList = new ArrayList<StyleInfo>();



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

                String style = jsonArray.getJSONObject(i).getString("style");
                String rate = jsonArray.getJSONObject(i).getString("rate");
                String beerID = jsonArray.getJSONObject(i).getString("id");

                int count = i + 1;

                style = count + ". " + style;


                //create object
                StyleInfo tempTaste = new StyleInfo(style, rate, beerID);

                //add to arraylist
                tasteList.add(tempTaste);


                //add items to listview
                StyleInfoAdapter adapter1 = new StyleInfoAdapter(c ,R.layout.brewer_stats_listview, tasteList);
                lv.setAdapter(adapter1);

                //set up clicks
                lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> arg0, View arg1,
                                            int arg2, long arg3) {
                        StyleInfo o=(StyleInfo)arg0.getItemAtPosition(arg2);

                        String bID = o.id;

                        //todo: add onclick for fragment to load


                        FragmentManager man= (Activity)c.getFragmentManager();
                        FragmentTransaction tran = man.beginTransaction();
                        Fragment_one = new StylePage2();
                        final Bundle bundle = new Bundle();
                        bundle.putString("beerIDSent", bID);
                        Fragment_one.setArguments(bundle);
                        tran.replace(R.id.main, Fragment_one);//tran.
                        tran.addToBackStack(null);
                        tran.commit();



                    }
                });


            }

        }
        catch(Exception e){

        }

        Dialog.dismiss();

    }

    public String readJSONFeed(String URL) {
        StringBuilder stringBuilder = new StringBuilder();
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(URL);
        try {
            HttpResponse response = httpClient.execute(httpGet);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if (statusCode == 200) {
                HttpEntity entity = response.getEntity();
                InputStream inputStream = entity.getContent();
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(inputStream));
                String line;
                while ((line = reader.readLine()) != null) {
                    stringBuilder.append(line);
                }
                inputStream.close();
            } else {
                Log.d("JSON", "Failed to download file");
            }
        } catch (Exception e) {
            Log.d("readJSONFeed", e.getLocalizedMessage());
        }
        return stringBuilder.toString();
    }

}

can not resolve method FragmentManager() is the error I am receiving
 

mrsegev

Member
Jan 22, 2012
19
4
Hi,
To be able to call getFragmentManager() or getSupportFragmentManager() you'll need to use FragmentActivity, so what I would do is:
1. Make sure the activity that calls the asycTask is a FragmentActivity.
2. Pass this actvity to the asycTask.

Something like: (asyncTask method)

setActivity(Activity activity)
{
fm = activity.getFragmentManager();
}
 

searayman

Member
Jun 19, 2013
12
1
Hi,
To be able to call getFragmentManager() or getSupportFragmentManager() you'll need to use FragmentActivity, so what I would do is:
1. Make sure the activity that calls the asycTask is a FragmentActivity.
2. Pass this actvity to the asycTask.

Something like: (asyncTask method)

setActivity(Activity activity)
{
fm = activity.getFragmentManager();
}

thats the problem, I launch the asyncTask from a fragment....