2

我使用FragmentPagerAdapter刷卡Fragment並使用AsyncTask來更新gridview,但未更新UI。片段中的AsyncTask未更新UI

它不是更新的用戶界面,也沒有拋我試圖檢查流量及其運行GalleryTab片段兩次的錯誤...和無法理解的問題。

我的代碼: -

public class GalleryTab extends Fragment { 
      GridView gridview; 
      ProgressDialog mProgressDialog; 
      GridViewAdapter adapter; 
      public List<GalleryList> phonearraylist = null; 
      View view; 
      private WeakReference<RemoteDataTask> asyncTaskWeakRef; 

     public static Fragment newInstance(Context context) { 
      GalleryTab f = new GalleryTab(); 
     return f; 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 
      // Inflate the layout for this fragment 
      view = inflater.inflate(R.layout.activity_gallery_tab, null); 
      setRetainInstance(true); 
       startNewAsyncTask(); 
      //new RemoteDataTask(this).execute(); 
      return inflater.inflate(R.layout.activity_gallery_tab, container, false); 
     } 

     // RemoteDataTask AsyncTask 
      private class RemoteDataTask extends AsyncTask<Void, Void, Void> { 
       private WeakReference<GalleryTab> fragmentWeakRef; 

       private RemoteDataTask (GalleryTab gallerytab) { 
        this.fragmentWeakRef = new WeakReference<GalleryTab>(gallerytab); 
       } 

       @Override 
       protected void onPreExecute() { 
        super.onPreExecute(); 

       } 

       @Override 
       protected Void doInBackground(Void... params) { 
        // Create the array 

        phonearraylist = new ArrayList<GalleryList>(); 
        try { 
         for (int i = 0; i <= 6; i++) { 
          GalleryList map = new GalleryList(); 
          map.setGallery("http://oi39.tinypic.com/21oydxs.jpg"); 
          System.out.println("PRINT!!!!-- "+ i); 
          phonearraylist.add(map); 
         } 
        } catch (ParseException e) { 
         Log.e("Error", e.getMessage()); 
         e.printStackTrace(); 
        } 

        return null; 
       } 

       @Override 
       protected void onPostExecute(Void result) { 
        // if (this.fragmentWeakRef.get() != null) { 
        adapter = new GridViewAdapter(getActivity(), phonearraylist); 
        System.out.println("PRINT SIZE -- "+ phonearraylist.size()); 
        gridview = (GridView)view.findViewById(R.id.gridview); 
        gridview.setAdapter(adapter); 

       // mProgressDialog.dismiss(); 
        // } 
       } 
      } 
      private void startNewAsyncTask() { 
       RemoteDataTask asyncTask = new RemoteDataTask(this); 
       this.asyncTaskWeakRef = new WeakReference<RemoteDataTask >(asyncTask); 
       asyncTask.execute(); 
      } 
    } 
+0

您所呼叫的AsyncTask兩次? 'startNewAsyncTask(); 新的RemoteDataTask(this).execute()'? – Raghunandan

回答

2

你得找出你的GridView onCreateView()方法,因爲它返回的視圖的ID ...

gridview = (GridView)view.findViewById(R.id.gridview); 

是的,如果你有做一個查看在intialize的對象,然後只需更換:

return inflater.inflate(R.layout.activity_gallery_tab, container, false); 

return view; 
+0

它沒有幫助.. –

+0

爲什麼你要執行RemoteDataTask兩次? – Piyush

+0

更新和刪除,但仍然沒有工作.. –

0

不知道這是特定的問題,但它似乎有很多RemoteDataTask範圍和GalleryTab之間編織的。嘗試將您的RemoteDataTask設置爲靜態,並且您失去引用的所有內容都應傳遞給RemoteDataTask構造函數。也許這會幫助你調試。

此:

private class RemoteDataTask extends AsyncTask<Void, Void, Void> 

要:

private static class RemoteDataTask extends AsyncTask<Void, Void, Void> 

這是處理多線程(這是AsyncTasks是什麼)的首選方式。

+0

其投擲: - 無法使靜態參考非靜態方法getActivity()從類型片段 –

+0

您會通過上下文'RemoteDataTask',你會保持它作爲一個弱引用或因爲你已經擁有的片段WeakReference的,你可以調用:'fragmentWeakRef.get()getActivity();' – JRomero

+0

我只是改變了(返回查看;)和它的工作對我來說...感謝支持.. –