我有一個從列表中輸出json數據的活動。但現在我想在一個片段中實現它。 在這個片段中,我想把它看作gridview。這兩個文件都能正常工作。但是當我試圖實現AsyncTask時,我得到了幾個redflags作爲不可達代碼。有人可以幫我解決這個問題嗎?在片段中實現AsyncTask android
編輯:新
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) {
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/sample.txt");
if(file.exists()) {
try{
Reader inputStreamReader = new InputStreamReader(new FileInputStream(file));
Gson gson = new Gson();
this.response = gson.fromJson(inputStreamReader, Response.class);
} 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.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){
gridView.setAdapter(new ImageAdapter(MainActivity.this,R.layout.grid_layout , imgArray, titleArray));
}
}
}
}
}
}
如何將圖像調用到上面的圖片GridView的?請幫忙。
也許張貼一些代碼?任何數量的問題都可能導致此問題。 – blaffie
在此發佈您的實施代碼............ – Piyush
@ Piyush Gupta&blaffie:現在添加的代碼。我想和片段中的活動做同樣的事情。 – Kabe