我正在尝试将JSON数据(图片网址)解析为gridView。我可以从JSON获取数据但是当我尝试设置simpleAdapter以在gridView上显示它时,我得到了一个红色标记:
构造函数SimpleAdapter(SalesFragment,List>,int,String [],int [])未定义。
我用Google搜索但找不到任何解决方案。请帮忙......
SalesFragment.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);
}
}
答案 0 :(得分: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()。