2017-02-11 120 views
-3

我想通過循環從ArrayList中獲取數據,但它顯示該列表爲空,並且arrayList.size()爲0即使我的adapter.getItemCount()也返回0但我的arrayList不是空的至少有2個或3個數據,它在RecyclerView完美放置,但仍然arrayList.size()是0ArrayList.size返回0但arryList不爲空

arrayList = Backgroundtask.getList(); 

    adapter = new RecycleAdapter(arrayList,getContext()); 

    ItemRecyclerView.setHasFixedSize(true); 
    layoutManager = new LinearLayoutManager(getActivity()); 
    ItemRecyclerView.setLayoutManager(layoutManager); 
    ItemRecyclerView.setAdapter(adapter); 

    for(int i = 0; i < arrayList.size(); i++){ 

     int quantity = arrayList.get(i).getQuantity(); 

     Total += quantity ; 
    } 
    Log.d("ArrayList Size " , String.valueOf(arrayList.size())); 
    Log.d("getItemCount Size " , String.valueOf(adapter.getItemCount())); 

這是我BackgroundTask:

public ArrayList<DataProvider> getList(){ 

    StringRequest stringRequest = new StringRequest(Request.Method.POST, jsonURL, 

      new Response.Listener<String>() { 
       @Override 
       public void onResponse(String response) { 

        try { 
         JSONArray jsonArray = new JSONArray(response); 

         for(int i = 0; i < jsonArray.length(); i++){ 
          JSONObject jsonObject = jsonArray.getJSONObject(i); 
          DataProvider dataProvider = new DataProvider(jsonObject.getString("Name"), 
            jsonObject.getDouble("quantity")); 
          arrayList.add(dataProvider); 
         } 
         UserFragment.adapter.notifyDataSetChanged(); 

        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 

       } 
      },new Response.ErrorListener(){ 
     @Override 
     public void onErrorResponse(VolleyError volleyError){ 


     } 
    }) { 
     @Override 
     protected Map<String, String> getParams() throws AuthFailureError { 

      Map<String,String> param = new HashMap<String,String>(); 
      param.put("id",MainActivity.USER_ID.toString()); 
      return param; 
     } 
    }; 

    Singletone.getSingletone(context).addToRequest(stringRequest); 
    return arrayList; 
} 
+0

'BackgroundTask.getList()'方法可以更新代碼? (int i = 0; i

+0

JSONObject jsonObject = jsonArray.getJSONObject(i); DataProvider dataProvider = DataProvider(jsonObject.getString(「Name」), \t jsonObject.getDouble(「Quantity」)); \t arrayList.add(dataProvider); }' –

+0

代碼不足以識別您的問題,在上面給出的代碼中,您的數組列表將僅爲空。請分享整個文件。 –

回答

1

所以它是一個愚蠢的錯誤,多數民衆贊成發生。您的請求和響應是異步的。那就是你的getList()方法返回空數組,因爲還沒有收到響應。但由於您在回覆中已寫入adapter.notifyDatasetChanged(),因此您可以在RecyclerView中看到這些項目。

編輯:

除了實現還有幾個內存泄漏。所以我在這裏更新代碼。

BlankFragment.java

public class BlankFragment extends Fragment implements BackgroundTask.UpdateListInterface{ 

    private Context mContext; 
    private List<DataProvider> arrayList; 
    private RecyclerView ItemRecyclerView; 
    private RecycleAdapter adapter; 

    public BlankFragment() { 
     // Required empty public constructor 
    } 

    @Override 
    public void onAttach(Context context) { 
     super.onAttach(context); 
     this.mContext = context; 
    } 

    @Override 
    public void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     arrayList = new ArrayList<>(); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     return inflater.inflate(R.layout.fragment_blank, container, false); 
    } 

    @Override 
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 
     ItemRecyclerView = view.findViewById(R.id.your_rc_view); 
    } 

    @Override 
    public void onActivityCreated(@Nullable Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     adapter = new RecycleAdapter(arrayList,getContext()); 
     ItemRecyclerView.setHasFixedSize(true); 
     ItemRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); 
     ItemRecyclerView.setAdapter(adapter); 
     BackgroundTask.updateList(this); 
    } 

    @Override 
    public void updateList(List<DataProvider> dataProviders) { 
     arrayList.addAll(dataProviders); 
     for(int i = 0; i < arrayList.size(); i++){ 
      int quantity = arrayList.get(i).getQuantity(); 
      Total += quantity ; 
     } 
     adapter.notifyDatasetChanged(); 
    } 

} 

BackgroundTask.java

public class BackgroundTask { 
    public static void updateList(final UpdateListInterface updateListInterface){ 
     final List<DataProvider> arrayList = new ArrayList<>(); 
     StringRequest stringRequest = new StringRequest(Request.Method.POST, jsonURL, 
       new Response.Listener<String>() { 
        @Override 
        public void onResponse(String response) { 

         try { 
          JSONArray jsonArray = new JSONArray(response); 

          for(int i = 0; i < jsonArray.length(); i++){ 
           JSONObject jsonObject = jsonArray.getJSONObject(i); 
           DataProvider dataProvider = new DataProvider(jsonObject.getString("Name"), 
             jsonObject.getDouble("quantity")); 
           arrayList.add(dataProvider); 
          } 

          //Here is how you must communicate with your calling class. 
          updateListInterface.updateList(arrayList); 
         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 

        } 
       },new Response.ErrorListener(){ 
      @Override 
      public void onErrorResponse(VolleyError volleyError){ 


      } 
     }) { 
      @Override 
      protected Map<String, String> getParams() throws AuthFailureError { 

       Map<String,String> param = new HashMap<String,String>(); 
       param.put("id",MainActivity.USER_ID.toString()); 
       return param; 
      } 
     }; 

     Singletone.getSingletone(context).addToRequest(stringRequest); 
    } 

    public interface UpdateListInterface{ 
     void updateList(List<DataProvider> dataProviders); 
    } 
} 
+0

那麼現在該怎麼辦?其實我是新來處理服務器請求.. –

+0

仍然有幾個內存泄漏,沒有人可以糾正他們,直到他們有完整的代碼。但暫時上面的代碼將解決您的問題。 –

相關問題