2

我正在尝试将 JSON 数据(图像 url)解析为 gridView。我能够从 JSON 中获取数据,但是当我尝试将 simpleAdapter 设置为在 gridView 上显示它们时,我得到了一个红旗说明:

构造函数 SimpleAdapter(SalesFragment, List>, int, String[], int[]) 未定义。

我用谷歌搜索但找不到任何解决方案。请帮忙...

销售片段.java

    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);
            //gridView.setAdapter(new ImageAdapter(this, getActivity()));
            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)  {

                    try{
                           // here getting JSON data using GSON
                        } 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>> aList1 = new ArrayList<HashMap<String,String>>();

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

                    if (sales.getCategories1().contains("12")){
                    //getting the data
                        }
                        aList1.add(hm);
                    }
                }

                SimpleAdapter adapter = new SimpleAdapter(SalesFragment.this, aList1,
                        R.layout.grid_sales, new String[] { "shop_image"},new int[] { R.id.sale_image });
                // updating gridview
                gridView.setAdapter(adapter);
           }
}
4

1 回答 1

4

你应该改变这个:

SimpleAdapter adapter = new SimpleAdapter(SalesFragment.this, aList1,
R.layout.grid_sales, new String[] { "shop_image"},new int[] { R.id.sale_image });

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

如果你已经为你的片段创建了一个上下文,那么你也可以使用那个而不是 getActivity()。

于 2013-09-04T12:32:53.693 回答