這是我正在開發的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沒有按照控制檯日誌被調用。
我有我的'setBillersList',但仍然不工作 –