@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mainActivity = (MainActivity) getActivity(); JsonObjectRequest request = new JsonObjectRequest( Request.Method.GET, ShekelNetwork.getInstance(getContext()).getAllReceiptsUrl(event), null, // no parameters post new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { // addHeader(); Gson gson = new Gson(); users = mainActivity.getUsers(); // todo maybe error (race) ShekelReceipt.ShekelReceiptModelContainer container = gson.fromJson( response.toString(), ShekelReceipt.ShekelReceiptModelContainer.class); for (ShekelReceipt.ShekelReceiptModel shekelReceiptModel : container.getData()) { ShekelReceipt shekelReceipt = new ShekelReceipt(); shekelReceipt.setId(shekelReceiptModel.getId()); shekelReceipt.setName(shekelReceiptModel.getName()); shekelReceipt.setCost(shekelReceiptModel.getCost()); shekelReceipt.setOwner(users.get(shekelReceiptModel.getOwner())); List<ShekelUser> userList = new ArrayList<>(); for (Integer userId : shekelReceiptModel.getConsumer_ids()) { userList.add(users.get(userId)); } shekelReceipt.setConsumers(userList); receiptList.add(shekelReceipt); } setListAdapter(new ListShekelNamedAdapter(getActivity(), receiptList)); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.d( "Receipt Fragment", "onErrorResponse() called with: " + "error = [" + error + "]"); } }); ShekelNetwork.getInstance(getContext()).addToRequestQueue(request); }
@Override public boolean onContextItemSelected(MenuItem item) { if (getUserVisibleHint()) { AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); ShekelReceipt receipt = (ShekelReceipt) getListAdapter().getItem((int) info.id); switch (item.getItemId()) { case R.id.action_add: mainActivity.addNewReceipt(event); return true; case R.id.action_delete: JsonObjectRequest request = new JsonObjectRequest( Request.Method.GET, ShekelNetwork.getInstance(getContext()).getUrlForDeleteReceipt(event, receipt), null, // no parameters post new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { mainActivity.showReceiptList(event); Log.d( "Receipt Fragment", "onErrorResponse() called with: " + "error = [" + response + "]"); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { mainActivity.showReceiptList(event); Log.d( "Receipt Fragment", "onErrorResponse() called with: " + "error = [" + error + "]"); } }); ShekelNetwork.getInstance(getContext()).addToRequestQueue(request); return true; case R.id.action_edit: mainActivity.changeReceipt(event, receipt); return true; default: return super.onContextItemSelected(item); } } return super.onContextItemSelected(item); }