11

में AsyncTask को लागू करें मेरे पास एक ऐसी गतिविधि है जो एक सूची के रूप में जेसन डेटा आउटपुट करती है। लेकिन अब मैं इसे एक टुकड़े में लागू करना चाहता हूं। इस खंड में मैं इसे ग्रिडव्यू के रूप में देखना चाहता हूं। और दोनों फाइलें ठीक काम करती हैं। लेकिन जब मैंने AsyncTask को लागू करने का प्रयास किया तो मुझे कई रेडफ्लैग पहुंचने योग्य कोड के रूप में प्राप्त नहीं हुआ। क्या कोई मेरी मदद कर सकता है कृपया?फ्रैगमेंट एंड्रॉइड

संपादित: न्यू

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

@ पियुष गुप्ता और ब्लैफी: कोड अब जोड़ा गया। मैं टुकड़े में गतिविधि के रूप में एक ही काम करना चाहता हूँ। – Kabe

उत्तर

20

activityCreateview

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

getActivity() पर टोकरा asyntask करने के लिए आसान कदम अपने टुकड़ा जगह कोड में टुकड़ा

भीतर पद

पर टुकड़ा और गतिविधि

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

है से पहले डेटा फ़ेच यह आवश्यक डेटा तो अनुकूलक में

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 
    } 

उपयोग हैश नक्शे के साथ

public class ImageModel 
    { 
     String title; 
     String img; 
    } 


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

भरने वालों की सूची की तरह मॉडल और छवियों स्थिति साथ आवंटित कर मॉडल

+0

धन्यवाद। आपके कोड ने मेरे प्रश्न के लिए मदद की, लेकिन अब मुझे नया मुद्दा है। मैं ग्रिडव्यू में 'shop_image' को कैसे कॉल कर सकता हूं? मैंने अपनी पोस्ट संपादित की, कृपया उपरोक्त कोड देखें। – Kabe

+0

आप एडाप्टर के कन्स्ट्रक्टर में छवि या एकाधिक छवियों को पारित कर सकते हैं मैंने ऊपर दिए गए कोड को संपादित किया है –

+0

यह काम नहीं किया गया है। mcontext और ImageAdapter पर redflags हो रही है। – Kabe

संबंधित मुद्दे