11

我有一個從列表中輸出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的?請幫忙。

+1

也許張貼一些代碼?任何數量的問題都可能導致此問題。 – blaffie

+1

在此發佈您的實施代碼............ – Piyush

+0

@ Piyush Gupta&blaffie:現在添加的代碼。我想和片段中的活動做同樣的事情。 – Kabe

回答

20

有簡單的步驟,以箱asyntask片段

內的片段代替代碼上activityCreateview

new MyAsyncTask(getActivity(), mListView).execute(""); 

getActivity()的方法上後

與片段和活動asynstak

溝通

context.mListView.setArrayAdapter(...........) 

這裏是

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); 
     } 

之前獲取數據,使其模擬像

public class ImageModel 
    { 
     String title; 
     String img; 
    } 


ArrayList<ImageModel> arrayList=new ArrayList<ImageModel>(); 

填充數組列表與所需的數據 然後在適配器

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 
    } 

使用哈希表,並與 位置分配圖像設置數據並從模型提取值

+0

謝謝。你的代碼有助於我的問題,但現在我有新問題。我怎麼能叫'shop_image'到一個gridview?我編輯了我的帖子,請參閱上面的代碼。 – Kabe

+0

你可以在適配器的構造函數中傳遞圖像或多個圖像我編輯了代碼,見上面 –

+0

它沒有工作。在mcontext和ImageAdapter上獲得紅旗。 – Kabe