0

這是我正在開發的Android應用程序的類。該recyclerView完美的作品的片段,但不是活動RecyclerView無法呈現Json數據

public class BillersActivity extends AppCompatActivity { 
VolleySingleton volleySingleton; 
RequestQueue requestQueue; 
private ArrayList<Billers> billersList= new ArrayList<>(); 
private RecyclerView recyclerView; 
private BillersAdaptor billersAdaptor; 
Context context; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.test_billers); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    getSupportActionBar().setHomeButtonEnabled(true); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    overridePendingTransition(R.layout.transition_in, R.layout.transition_out); 

    sendJsonRequest(); 

    recyclerView = (RecyclerView)findViewById(R.id.testbillersListRecyclerView); 
    billersAdaptor = new BillersAdaptor(this, billersList);// 
    recyclerView.setAdapter(billersAdaptor); 
    recyclerView.setLayoutManager(new LinearLayoutManager(context)); 
    recyclerView.setHasFixedSize(true); 


private void sendJsonRequest() { 
    L.m("sending billers request"); 
    StringRequest activity =new StringRequest(Request.Method.GET, Url.getBillersUrl() , new Response.Listener<String>() { 
     @Override 
     public void onResponse(String response) { 
      billersList = parseJsonResponse(response); 
      billersAdaptor.setBillersList(billersList); 
     } 
    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      VolleyCustomErrorHandler.errorMessage(error); 
     } 
    }); 

    requestQueue = volleySingleton.getInstance().getRequestQueue(); 
    requestQueue.add(activity); 
} 



private ArrayList<Billers> parseJsonResponse(String response) { 

    ArrayList<Billers> billersList = new ArrayList<>(); 

    if(response !=null||response.length() !=0) { 


     try { 
      //Creating JsonObject from response String 
      JSONObject jsonObject = new JSONObject(response); 
      //extracting json array from response string 
      JSONArray billersArray = (JSONArray) jsonObject.getJSONArray("response"); 

      for (int i = 0; i < billersArray.length(); i++) { 

       JSONObject jsonRow = billersArray.getJSONObject(i); 
       //get value from jsonRow 
       String name = (String) jsonRow.get("name"); 
       String description = (String) jsonRow.get("description"); 
       Billers billers = new Billers(); 
       billers.setName(name); 
       billers.setDescription(description); 

       billersList.add(billers); 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 

    return billersList; 
} 

}

,這是我的適配器

public class BillersAdaptor extends RecyclerView.Adapter<BillersAdaptor.ViewHolderBillers>{ 
private LayoutInflater layoutInflater; 
private ArrayList<Billers> billersList = new ArrayList<>(); 

public BillersAdaptor(Context context, ArrayList<Billers> billersList){ 
    layoutInflater = LayoutInflater.from(context); 
    this.billersList=billersList; 
} 

public void setBillersList(ArrayList<Billers> billersList){ 
    this.billersList=billersList; 
    notifyItemRangeChanged(0, billersList.size()); 

} 

@Override 
public ViewHolderBillers onCreateViewHolder(ViewGroup parent, int viewType) { 

    L.m("ayoutInflater.inflate(R.layout.item_billers_list, parent, false);"); 
    View layout = layoutInflater.inflate(R.layout.item_billers_list, parent, false); 
    ViewHolderBillers viewHolderBillers = new ViewHolderBillers(layout); 
    L.m("creating billers"); 
    return viewHolderBillers; 
} 

@Override 
public void onBindViewHolder(ViewHolderBillers holder, int position) { 
    L.m("Billers current = billersList.get(position)"); 
    Billers current = billersList.get(position); 

    holder.billing_name.setText(current.getName()); 
    holder.billing_desc.setText(current.getDescription()); 
    L.m("Binding billers view holders"); 

} 

@Override 
public int getItemCount() { 
    return billersList.size(); 
} 


static class ViewHolderBillers extends RecyclerView.ViewHolder{ 
    private TextView billing_desc; 
    private TextView billing_name; 

    public ViewHolderBillers(View itemView) { 
     super(itemView); 
     L.m("creating billers view holder"); 
     billing_desc = (TextView) itemView.findViewById(R.id.billing_desc); 
     billing_name = (TextView) itemView.findViewById(R.id.billing_name); 
    } 
} 

}

問題是,它的負荷,我可以登錄json迴應沒問題,但是onCreateViewHolder,onBindViewHolder和整個ViewHolder分類s沒有按照控制檯日誌被調用。

回答

0

我想你應該billersAdaptor.notifyDataSetChanged();更新您的適配器的項目,當這種情況發生時,使它們

+0

我有我的'setBillersList',但仍然不工作 –

0

的問題是,你正在改變你的活動ArrayList中的參考,這就是錯誤的無效的意見。您應該首次添加到ArrayList,因爲如果您沒有這樣做,那麼您正在編輯的對象將與適配器所持有的對象不同。

換句話說,parseJsonResponse不應該創建一個新的ArrayList,它應該添加到舊的。並改變它之後,你應該通知你已經使用

billersAdaptor.notifyDataSetChanged(); 
0

改變你的setBillersList改變它的底層數據youradapter這個

public void setBillersList(ArrayList<Billers> billersList){ 
    this.billersList.addAll(billersList);//you can even clear before this line 
    notifyDataSetChanged(); 
} 
0

這是因爲你調用notifyItemRangeChanged()適配器。

答案是here

添加一些邏輯來檢查已更改,插入或刪除了什麼,並適當地調用其餘的「通知」調用。