Android 如何在片段中设置gridview适配器
嗨,我在片段中找到了AsyncTask,它从JSON字符串获取图像url。现在我想把它们放到gridview中。我可以在活动中这样做,但不能在片段中这样做。有人能帮我解决吗 我在这里发布了类似的问题:。并找到了在片段中使用asynctask的答案,但没有找到GridView的答案 我的代码:Android 如何在片段中设置gridview适配器,android,Android,嗨,我在片段中找到了AsyncTask,它从JSON字符串获取图像url。现在我想把它们放到gridview中。我可以在活动中这样做,但不能在片段中这样做。有人能帮我解决吗 我在这里发布了类似的问题:。并找到了在片段中使用asynctask的答案,但没有找到GridView的答案 我的代码: public class SalesFragment extends Fragment { GridView gridView; @Override public View onC
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
}
}
}
}
}
}
}
公共类SalesFragment扩展了片段{
GridView;
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图gv=充气机。充气(R.layout.hot_sales,空);
gridView=(gridView)gv.findViewById(R.id.grid\u视图);
bindGridview();
返回gv;
}
public void bindGridview(){
新建MyAsyncTask(getActivity(),gridView)。执行(“”);
}
类MyAsyncTask扩展了AsyncTask{
GridView-mGridView;
活动mContext;
反应;
公共MyAsyncTask(活动上下文,GridView gview){
this.mGridView=gview;
this.mContext=上下文;
}
受保护的字符串doInBackground(字符串…参数){
if(file.exists()){
试一试{
//解析数据
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(@SuppressWarnings(“隐藏”)IOE异常){
e、 printStackTrace();
}
}否则{
System.out.println(“错误”);
}
返回null;
}
@凌驾
受保护的void onPostExecute(字符串结果){
super.onPostExecute(结果);
//列表列表=新的ArrayList();
for(销售:this.response.Sales){
HashMap hm=新的HashMap();
if(销售分类1){
//获取数据
}
}
}
}
}
}
}
找到了答案。下面的代码解决了这个问题
SimpleAdapter adapter = new SimpleAdapter(getActivity(), aList1,
R.layout.grid_sales, new String[] { "shop_image"},new int[] { R.id.sale_image });
更新片段中的gridView必须在片段的onCreateView中完成@特温特:怎么做?你能再解释一下吗