2015-05-19 63 views
0

我想填充片段列表查看使用異步任務, 但是listvew不填充。我正在獲取日誌中的數據,並且日誌中沒有錯誤和異常。數據不填充列表中,Android

我下面這個例子

dynamic listview adding "Load more items" at the end of scroll

這裏是代碼: -

public class HindiFragment extends Fragment { 
     // XML node keys 
      static final String KEY_SONG = "song"; // parent node 
      static final String KEY_ID = "id"; 
      static final String KEY_TITLE = "title"; 
      static final String KEY_ARTIST = "artist"; 
      static final String KEY_DURATION = "duration"; 
      static final String KEY_THUMB_URL = "thumb_url"; 
      static final String KEY_VIDEO_URL = "video"; 
      static final String KEY_UPLOAD_BY = "upload_by"; 
      Context abc=null; 
      static int startIndex = 0; 
      private WeakReference<MyAsyncTask> asyncTaskWeakRef; 
      ListView list; 
      static LazyAdapter adapter; 
      JSONObject json; 
      static int offset = 10; 
      static ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>(); 

      public static Context hindiFragment=null; 
      private static int catId=0; 
      static View rootView ; 
     public HindiFragment(){} 

     public HindiFragment(int position) { 
      // TODO Auto-generated constructor stub 
      catId=position; 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 
      //StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 

      //StrictMode.setThreadPolicy(policy); 


      rootView = inflater.inflate(R.layout.main, container, false); 
       // TextView tvLabel = (TextView)rootView.findViewById(R.id.txtLabel); 
       // tvLabel.setText("Hello"); try 
      hindiFragment=rootView.getContext(); 
      try{ 
       //Toast.makeText(hindiFragment, "catid is "+catId, 
        //  Toast.LENGTH_LONG).show(); 
       UserFunctions userFunction = new UserFunctions(); 
       json=userFunction.getAndroidVersion(); 
       JSONArray android_version_array = json.getJSONArray("version"); 
       TextView tv = (TextView) rootView.findViewById(R.id.android_version); 
       //getting android version 
       for (int i = 0; i < android_version_array.length(); i++) { 
         // creating new HashMap 
         HashMap<String, String> map = new HashMap<String, String>(); 
         JSONObject myObj = android_version_array.getJSONObject(i); 
         if(! myObj.getString("version").equalsIgnoreCase(String.valueOf(getString(R.string.android_version)))){ 
         tv.setText(Html.fromHtml(myObj.getString("text"))); 
         tv.setMovementMethod(LinkMovementMethod.getInstance()); 
         }else 
         { 
          tv.setVisibility(View.GONE); 
         } 
         // adding each child node to HashMap key => value 
        } 

       startNewAsyncTask(this.getActivity()); 

      return rootView; 
      }catch(Exception e){ 
       e.printStackTrace(); 
      } 
      return rootView; 

     } 

     public static void loadMore(int startIndex,int page,Activity myActivity){ 
      try{ 
      UserFunctions userFunction = new UserFunctions(); 
      Log.e("page ",""+page); 
      int status=0; 
      JSONObject json; 
      json = userFunction.getChannelData(String.valueOf(catId),page); 
      if(json.has("video")){ 
      JSONArray deletedtrs_array = json.getJSONArray("video"); 

      for (int i = 0; i < deletedtrs_array.length(); i++) { 
       // creating new HashMap 
       HashMap<String, String> map = new HashMap<String, String>(); 
       JSONObject myObj = deletedtrs_array.getJSONObject(i); 
       // adding each child node to HashMap key => value 
       map.put(KEY_ID, myObj.getString("uid")); 
       map.put(KEY_TITLE, myObj.getString("uid")); 
       map.put(KEY_ARTIST,myObj.getString("video")); 
       map.put(KEY_DURATION, myObj.getString("duration")); 
       map.put(KEY_THUMB_URL,myObj.getString("thumb_url")); 
       map.put(KEY_VIDEO_URL, myObj.getString("url")); 
       map.put(KEY_UPLOAD_BY,"By: "+ myObj.getString("upload_by")); 

       // adding HashList to ArrayList 
       //if(!songsList.contains(map)) 
       { 
       songsList.add(map); 
       status=1; 
       } 
      } 
      } 
      //if(status==1) 
      { 
      //]list=(ListView)rootView.findViewById(R.id.list); 
      // Getting adapter by passing xml data ArrayList 
      // adapter=new LazyAdapter(myActivity, songsList);  
      // list.setAdapter(adapter); 
      // adapter.notifyDataSetChanged(); 
      } 
      }catch(Exception e){ 

       e.printStackTrace(); 
      } 

     } 
     private void startNewAsyncTask(Activity act) { 
      MyAsyncTask asyncTask = new MyAsyncTask(act); 
      this.asyncTaskWeakRef = new WeakReference<MyAsyncTask >(asyncTask); 
      asyncTask.execute(); 
     } 
     private static class MyAsyncTask extends AsyncTask<Void, Void, Void> { 

      private WeakReference<HindiFragment> fragmentWeakRef; 
      HindiFragment uindiFragment; 
      Activity myActivity; 
      private MyAsyncTask (Activity activity) { 
       this.fragmentWeakRef = new WeakReference<HindiFragment>(uindiFragment); 
       myActivity=activity; 
      } 

      @Override 
      protected Void doInBackground(Void... params) { 
       //Toast.makeText(hindiFragment, "helllo", 
        // Toast.LENGTH_LONG).show(); 
          //TODO: your background code 
       Log.e("Now in background",offset+""); 
       loadMore(startIndex, offset,myActivity); 
       return null; 
      } 

      @Override 
      protected void onPostExecute(Void response) { 
       super.onPostExecute(response); 
       ListView list=(ListView)rootView.findViewById(R.id.list); 
       // Getting adapter by passing xml data ArrayList 
       adapter=new LazyAdapter(myActivity, songsList); 
       list.setAdapter(adapter); 
       adapter.notifyDataSetChanged(); 
       Log.e("Ended here11 ","now endeded11"); 

       if (this.fragmentWeakRef.get() != null) { 
           //TODO: treat the result 
        adapter.notifyDataSetChanged(); 
       } 
      } 
     } 


    } 
+0

請在帖子正文中澄清您的問題 –

+0

很明顯。我正在嘗試使用異步任務填充listview,但沒有顯示帶有數據的列表。一個空白頁面正在顯示。如果我不使用異步任務,則顯示listview中的數據。 – neooo

回答

0

您是過度使用弱引用。我的猜測是你的異步任務或碎片被垃圾收集,因爲你週期性地使用它們:你可以通過看看weakReference.get()是否返回null來驗證。

事實上,根本不需要WeakReference:只需實施適當的取消邏輯,您可以從onPause onStoponDestroy調用取決於您想要如何處理重複呼叫。

而且你不應該上下文傳遞到您的Async類:而是創建一個觀察者,如: public interface Callback { public void onAsyncDone(Arraylist<DataItem> listItems); } ,如果不取消, @Override public void onPostExecute(Void v) { if (!isCancelled()) { Callback c = callbackReference.get(); if (c != null) c.onAsyncDone(getListItems()); } } 其中getListItems()回報什麼工作doInBackground完成。在這裏,我使用了一個弱回調函數(在您的活動中實現),因爲您似乎想使用它。但是,再次,只要你釋放對上下文的引用(如果取消),你實際上並不需要一個引用。活動被封裝爲Callback的原因是異步任務應該只有一個作業:處理這些項目並將它們傳遞給它們。讓您的活動或控制器用新項目更改UI。

+0

是的,你是對的,fragmentWeakRef.get()返回null,我該如何解決這個問題? – neooo

+0

我評論了所有weakrefrences行,但列表仍然沒有顯示數據。 – neooo

+0

而我將活動傳遞給asynctask。 MyAsyncTask asyncTask = new MyAsyncTask(act); – neooo

0

我懷疑loadMore()方法不會加載任何數據到對象songsList到適配器中。簡單地追溯它不是的原因。我無法在不使用調試器的情況下調試此代碼,代碼流非常複雜,引用異常嚴格,更重要的是,使用靜態訪問。 審查可疑代碼:

protected void onPostExecute(Void response) { 
    super.onPostExecute(response); 
    ListView list=(ListView)rootView.findViewById(R.id.list); 
    // Getting adapter by passing xml data ArrayList 
    adapter=new LazyAdapter(myActivity, songsList); 
    list.setAdapter(adapter); 
    adapter.notifyDataSetChanged(); 
... 
} 

注意:您可以把一些好的日誌中LazyAdapter代碼,看看數據是否被填充在那裏。