onPostExecute in Fragment not working (works in Activity)

1.2k views Asked by At

My problem: This code doesn't work in my activity I am trying to implement a gridView adapter that gets data from parse.com

@Override
    protected void onPostExecute(Void result) {
        // Locate the gridview in gridview_main.xml
        gridview = (GridView) findViewById(R.id.gridview);
        // Pass the results into ListViewAdapter.java
        adapter = new GridViewAdapter(MainActivity.this,
                phonearraylist);
        // Binds the Adapter to the ListView
        gridview.setAdapter(adapter);
        // Close the progressdialog
        mProgressDialog.dismiss();
    }
}
}

This is within my "DiscoverActivity" class that exteds SherlockFragment

public class DiscoverActivity extends SherlockFragment

// Declare Variables
GridView gridview;
List<ParseObject> ob;
ProgressDialog mProgressDialog;
ImageAdapter adapter;
private List<ImageList> phonearraylist = null;

@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.activity_discover,container,false);
//        GridView gridView = (GridView) view.findViewById(R.id.gridview);
 //        gridView.setAdapter(new ImageAdapter(view.getContext())); // uses the view to get the context instead of getActivity().
    new RemoteDataTask().execute();
    return view;
}

private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();

//            Toast.makeText(getActivity(), "hello",Toast.LENGTH_LONG).show();
        Toast.makeText(getActivity().getApplicationContext(),"Loading...",Toast.LENGTH_LONG).show();

    }

protected Void doInBackground(Void... params) {
    // Create the array
    phonearraylist = new ArrayList<ImageList>();
    try {
        // Locate the class table named "SamsungPhones" in Parse.com
        ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
                "testData2");
        // Locate the column named "position" in Parse.com and order list
        // by ascending
        query.orderByAscending("Price");
        ob = query.find();
        for (ParseObject country : ob) {
            ParseFile image = (ParseFile) country.get("Photo");
            ImageList map = new ImageList();
            map.setPhone(image.getUrl());
            phonearraylist.add(map);
        }
    } catch (ParseException e) {
        Log.e("Error", e.getMessage());
        e.printStackTrace();
    }
    return null;
}

//Here is what I have tried so far...
@Override
protected void onPostExecute(Void result) {
    // Locate the gridview in gridview_main.xml
    gridview = (GridView) findViewById(R.id.gridview);
    // Pass the results into ListViewAdapter.java
    //adapter = new ImageAdapter(DiscoverActivity.this,
      //      phonearraylist);
    GridView gridView = (GridView) view.findViewById(R.id.gridview);
    gridView.setAdapter(new ImageAdapter(view.getContext())); // uses the view to get the context instead of getActivity().
    // Binds the Adapter to the ListView
    gridview.setAdapter(adapter);
    // Close the progressdialog
    mProgressDialog.dismiss();
}
}
}

I am undable to findViewById in my onPostExecute

Thanks in advance for any help you can give me!

2

There are 2 answers

0
user2953680 On

have you wrote this statement in the onCreate method? (of the activity)

setContentView(R.layout.some_activity);

then you can use all id's defined there.

0
Scott On

In the example you have referenced the parent class is Activity which has a method findViewById() your parent class SherlockFragment does not. Therefore, you need to get a reference to the gridView using the layout that you inflate in onCreate() (as you were doing), I say this assuming that the GridView is in the layout.

Because your AsyncTask is an inner class it has access to the instance variables of the outer class (your fragment class) you can create a pointer to the GridView in onCreate() and use it in onPostExecute()