0

Hi I've AsyncTask in Fragment which gets image url from JSON string. Now I want to put them into a gridview. I'm able to do this on Activity but not in Fragment. Can someone help me to solve it?

I posted similar question here: implement AsyncTask in Fragment android. And found the answer for using asynctask in fragment, but not for GridView.

My code:

public class SalesFragment extends Fragment {
    GridView gridView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View gv = inflater.inflate(R.layout.hot_sales, null);
        gridView = (GridView) gv.findViewById(R.id.grid_view);
        bindGridview();
        return gv;
    }

    public void bindGridview() {

       new MyAsyncTask(getActivity(),gridView).execute("");
    }

    class MyAsyncTask extends AsyncTask<String, String, String> {
        GridView mGridView;
        Activity mContext;
        Response response;
       public  MyAsyncTask(Activity context,GridView gview) {
         this.mGridView=gview;
         this.mContext=context;
        }

       protected String doInBackground(String... params)  {


            if(file.exists()) {
                try{
                       //Parsing data
                    } catch (FileNotFoundException e) {
                       e.printStackTrace();
                    } catch (@SuppressWarnings("hiding") IOException e){
                       e.printStackTrace();
                    }
            }else{
                System.out.println("Error");
            }
            return null;
            }

       @Override
       protected void onPostExecute(String result) {

            super.onPostExecute(result);

            //List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();

            for(Sales sales : this.response.sales){
                HashMap<String, String> hm = new HashMap<String,String>();

                if (sales.Categories1){
                    //Fetching the data
                        }
                    }
                }
            }
       }
    }
}
Community
  • 1
  • 1
Kabe
  • 223
  • 2
  • 4
  • 16

1 Answers1

0

Found the answer. Following code solved it.

SimpleAdapter adapter = new SimpleAdapter(getActivity(), aList1,
R.layout.grid_sales, new String[] { "shop_image"},new int[] { R.id.sale_image });
Kabe
  • 223
  • 2
  • 4
  • 16