0

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();
}
wkh
  • 35
  • 1
  • 4
  • you defiuned `listTransactions=new ArrayList<>();` in `getTransactions` function, how can you expect to use it outside in `initializeAdapter`? – Mehran Zamani Feb 12 '17 at 15:51
  • I defined it global ArrayList listTransactions; That was the initialization – wkh Feb 12 '17 at 16:02
  • :D sorry, my mistake. i think you should set adapter first then initialize your array. that's the right way. – Mehran Zamani Feb 12 '17 at 16:07
  • I've tried but still nothing, the problem is that onSuccess seems like it doesn't change global variables, I even tried to put "response" inside a global String variable and still nothing, it only shows it inside – wkh Feb 12 '17 at 16:32

1 Answers1

0

The onSuccess is not yet executed when you call initializeAdapter(), because it is asynchronous. You should initialize your adapter onSuccess or notifyDataSetChanged() after adding the elements in the ArrayList.

Monika Bozhinova
  • 294
  • 3
  • 16