I have a global arrayList which I am using for showing android cardViews
ArrayList<Transaction> listTransactions;
When using listTransactions.add inside onSuccess of AsyncHttpClient it works fine, but out of onSuccess function (inside initializeAdapter) the listTransactions becomes empty
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getTransactions();
initializeAdapter();
}
private void initializeAdapter(){
Transaction_Adapter adapter = new Transaction_Adapter(listTransactions);
transaction_card.setAdapter(adapter);
LinearLayoutManager llm = new LinearLayoutManager(getContext());
transaction_card.setLayoutManager(llm);
transaction_card.setHasFixedSize(true);
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(transaction_card.getContext(),1);
transaction_card.addItemDecoration(dividerItemDecoration);
}
public void getTransactions() {
listTransactions=new ArrayList<>();
AsyncHttpClient client = new AsyncHttpClient();
client.get(ip,params ,new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String response) {
try {
JSONObject obj = new JSONObject(response);
if(obj.getBoolean("status")){
int count = obj.getInt("count");
for(int i=0;i<count;i++) {
JSONObject o = obj.getJSONObject("Transaction_" + i);
listTransactions.add(new Transaction(o.getInt("Sender"),o.getInt("Receiver"),o.getDouble("Amount"),o.getString("Date"),o.getString("Type")));
}
//testing
for(Transaction t:listTransactions){
Log.e("transaction",String.valueOf(t.type)); //this shows the type of all transactions correctly
}
}
.
.
.
}
}//catch & onFailure...
});}
There is no exception or errors.
Also When I try to add it manually before the initializeAdapter(), it shows the manually added card fine
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getTransactions();
listTransactions.add(new Transaction(1,2,111,"01-12-2016 12:15","test")); //manually added card
initializeAdapter();
}