11

Tôi đã tạo hoạt động xuất dữ liệu json từ danh sách. Nhưng bây giờ tôi muốn thực hiện nó trong một mảnh. Trong đoạn này, tôi muốn xem nó như là GridView. Và cả hai tệp đều hoạt động tốt. nhưng khi tôi cố gắng triển khai AsyncTask, tôi nhận được một số thẻ đỏ dưới dạng mã không thể truy cập được. Xin vui lòng giúp tôi việc này.triển khai AsyncTask trong Fragment android

được sửa đổi: New

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



      } 
     } 
    } 

Làm thế nào để gọi hình ảnh vào GridView trên hình trên? Hãy giúp tôi.

+1

Có lẽ đăng một số mã? Bất kỳ số lượng các vấn đề có thể gây ra điều này. – blaffie

+1

đăng mã được triển khai của bạn tại đây ............ – Piyush

+0

@ Piyush Gupta & blaffie: mã được thêm ngay bây giờ. Tôi muốn làm điều tương tự như hoạt động trong phân đoạn. – Kabe

Trả lời

20

Có dễ dàng bước tới thùng asyntask trong đoạn

tại chỗ đoạn mã của bạn trên activityCreateview

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

getActivity() được phương pháp để giao tiếp với mảnh và hoạt động

trong asynstak về bài đăng

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

đây là

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

trước khi dữ liệu lấy làm cho nó mô hình như array list

public class ImageModel 
    { 
     String title; 
     String img; 
    } 


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

điền với dữ liệu cần thiết sau đó

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 
    } 

sử dụng bản đồ băm trong adapter và gán hình ảnh với vị trí đặt dữ liệu và tìm nạp giá trị từ mô hình

+0

Cảm ơn. Mã của bạn đã giúp cho câu hỏi của tôi, nhưng bây giờ tôi đã có vấn đề mới. Làm thế nào tôi có thể gọi 'shop_image' vào GridView? Tôi đã chỉnh sửa bài đăng của mình, Vui lòng xem mã ở trên. – Kabe

+0

bạn có thể chuyển hình ảnh hoặc nhiều hình ảnh trong hàm dựng của bộ điều hợp i đã chỉnh sửa mã xem ở trên –

+0

nó không hoạt động. nhận redflags trên mcontext và ImageAdapter. – Kabe

Các vấn đề liên quan