This is how my ListFragment
looks
public class TransactionListFragment extends ListFragment {
private List<Transaction> mTransactions;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().setTitle(R.string.transactions);
mTransactions = Transactions.get(getActivity()).getTransactionsFromServer();
ArrayAdapter<Transaction> adapter = new ArrayAdapter<>(getActivity(),
android.R.layout.simple_list_item_1, mTransactions);
setListAdapter(adapter);
}
}
and Transactions.get(getActivity()).getTransactionsFromServer();
looks like
private void getTransactionsFromServer() {
final String url = "myURL";
RequestQueue queue = Volley.newRequestQueue(mContext);
JsonObjectRequest request = new JsonObjectRequest(url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("GET /Transactions:", response.toString());
generateTransactionCollectionFromResponse(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// handle error
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<>();
headers.put("BEARER", "55b885274e7912280095ef80ac1cb937:d8922b44-75af-4810-a87e");
return headers;
}
};
queue.add(request);
}
private void generateTransactionCollectionFromResponse(JSONObject response) {
JSONArray transactionsJson = null;
try {
transactionsJson = response.getJSONArray("transactions");
Log.d("TransactionsJson:", transactionsJson.toString());
for (int i = 0; i < transactionsJson.length(); i++) {
JSONObject transactionJson = transactionsJson.getJSONObject(i);
Transaction transaction = new Transaction(transactionJson.getString("id"), transactionJson.getString("name"));
mTransactions.add(transaction);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
Given that the task will happen asynchronously, the ListView
would be empty initially.
Question
How do I indicate from my Transactions
that reload the listView once I am done getting the results?
UPDATE
This is how Transaction
is constructed
public class Transactions {
private List<Transaction> mTransactions;
private static Transactions sTransactions;
private Context mContext;
private Transactions(Context appContext) {
mContext = appContext;
mTransactions = new ArrayList<>();
}
Question
How can I get Adapter
or ListView
?