There is easy step to crate asyntask within fragment
in your fragment place code on activityCreateview
new MyAsyncTask(getActivity(), mListView).execute("");
getActivity()
is method to communicate with fragment and activity
in asynstak on post
context.mListView.setArrayAdapter(...........)
here is
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;
//return super.onCreateView(inflater, container, savedInstanceState);
}
public void bindGridview()
{
new MyAsyncTask(getActivity(), gridView).execute("");
}
class MyAsyncTask extends AsyncTask<String, String, String>
{
GridView mGridView;
Activity mContex;
public MyAsyncTask(Activity contex,GridView gview)
{
this.mGridView=gview;
this.mContex=contex;
}
protected String doInBackground(String... params)
{
//fetch data
}
@Override
protected void onPostExecute(String result) {
{
for(Sales sales : this.response.sales){
HashMap<String, String> hm = new HashMap<String,String>();
if (sales.getCategories1().contains("12")){
//hm.put("sale_title", "" + sales.getShort_title());
for(Shop shop : this.response.shops){
String image_file = new String( Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/images/" + shop.getImage());
if(shop.getId().equals(sales.getShop_id())){
hm.put("shop_image", image_file );
System.out.println(shop_image);
}
}
}
}
if(hm.size()>0)
mcontext.mGridView.setAdapter(new ImageAdapter(mContext),hm);
}
before fetching data make it model like
public class ImageModel
{
String title;
String img;
}
ArrayList<ImageModel> arrayList=new ArrayList<ImageModel>();
fill array list with required data
then
mcontext.mGridView.setAdapter(new ImageAdapter(mContext),hm,arrayList);
//in image adapter
public class ImageAdapter extends ArrayAdapter<String> {
public ImageAdapter(Context context, HashMap<String, String> hm,ArrayList<ImageModel> images )
{
super(context, R.layout.activity_t);
}
//--code
}
use hash map in adapter and assign images with position
set data and fetch value from model
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…