I'm fairly new to android and java, I'm pulling some data from the internet by clicking on an item in a list view, but I need 2 Calls to get the information I need. When I get the int value from the first call i pass it to the second method which does its own call, gets a value, and puts it in SharedPref. When I try to retrieve the data back in the first method, first it return the default value of "", but when I click on the second item it shows the result that should have been called the first time, on the third click it shows the second result etc...

I tried with databases, and now I am trying with SharedPref, always the same result. Tried putting the method call in a thread, still the same...

This is my first method which calls the second called spendingCategory

private void listViewFunction() {
        lvItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int userId = arrayList.get(position).idUser;
                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl(URL)
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();

                TrikoderAPI trikoderAPI = retrofit.create(TrikoderAPI.class);
                Call<SingleFeed> call = trikoderAPI.getSingleFeed(userId);
                call.enqueue(new Callback<SingleFeed>() {
                    @Override
                    public void onResponse(Call<SingleFeed> call, Response<SingleFeed> response) {
                        SingleFeed data = response.body();

                        final int categoryId = data.getData().getRelationships().getSpendingCategory().getData().getId();
                        spendingCategory(categoryId);

                        String info;
                        info = sp.getString(CATEGORY_NAME, "");

                        String result = getString(R.string.type) + data.getData().getType() + "\n"
                                + getString(R.string.id) + data.getData().getId() + "\n"
                                + getString(R.string.amount) + data.getData().getAttributes().getAmount() + "\n"
                                + getString(R.string.remark) + data.getData().getAttributes().getRemark() + "\n"
                                + getString(R.string.name) + data.getData().getAttributes().getName() + "\n"
                                + getString(R.string.date) + data.getData().getAttributes().getDate() + "\n"
                                + getString(R.string.category) + info;

                        popUpWindow(result);
                        editor.clear();
                        editor.commit();

                    }

                    @Override
                    public void onFailure(Call<SingleFeed> call, Throwable   t) {
                        Toast.makeText(MainActivity.this, getString(R.string.somethingWrong) + t.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }

And this is the second method

private void spendingCategory(int categoryId) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        TrikoderAPI trikoderAPI = retrofit.create(TrikoderAPI.class);
        Call<SingleCategory> call = trikoderAPI.getCategoryFeed(categoryId);
        call.enqueue(new Callback<SingleCategory>() {
            @Override
            public void onResponse(Call<SingleCategory> call, Response<SingleCategory> response) {
                SingleCategory data = response.body();

                String result = data.getData().getAttributes().getName();
                Log.d(TAG, "onResponse: " + result);

                sp.edit().putString(CATEGORY_NAME, result).apply();

            @Override
            public void onFailure(Call<SingleCategory> call, Throwable t) {
                Toast.makeText(MainActivity.this, getString(R.string.somethingWrong), Toast.LENGTH_SHORT).show();
            }
        });
    }

I expect the first output to be the value of the result String, and not the default value I get from SharedPref

2 Answers

0
Ankit ihelper Sharma On Best Solutions

SharedPreference.apply() - This method asynchronously (later) save changes.
SharedPreference.commit() - This method synchronously (instantly).

Retrofit enqueue() is asynchronus call. So, you cannot sure for order of execution.

For your problem, you must perform action after second method call response i.e. spendingCategory

Change method to

spendingCategory(int categoryId,SingleFeed data)

add below code inside onResponse()

...
  String result = data.getData().getAttributes().getName();
                Log.d(TAG, "onResponse: " + result);

                sp.edit().putString(CATEGORY_NAME, result).commit();
String info;
info = sp.getString(CATEGORY_NAME, "");

String result = getString(R.string.type) + data.getData().getType() + "\n"
                + getString(R.string.id) + data.getData().getId() + "\n"
                + getString(R.string.amount) + 
                data.getData().getAttributes().getAmount() + "\n"
               + getString(R.string.remark) + 
               data.getData().getAttributes().getRemark() + "\n"
               + getString(R.string.name) + 
              data.getData().getAttributes().getName() + "\n"
              + getString(R.string.date) + 
               data.getData().getAttributes().getDate() + "\n"
               getString(R.string.category) + info;

                        popUpWindow(result);
                        editor.clear();
                        editor.commit();

I think by above implementation, you won't required shared preference for above call.

0
Rajnish suryavanshi On

Check With this :

spendingCategory(categoryId,data);

Now, Move below code inside network call of spending category, put this code :

                   String result = getString(R.string.type) + data.getData().getType() + "\n"
                            + getString(R.string.id) + data.getData().getId() + "\n"
                            + getString(R.string.amount) + data.getData().getAttributes().getAmount() + "\n"
                            + getString(R.string.remark) + data.getData().getAttributes().getRemark() + "\n"
                            + getString(R.string.name) + data.getData().getAttributes().getName() + "\n"
                            + getString(R.string.date) + data.getData().getAttributes().getDate() + "\n"
                            + getString(R.string.category) + info;



                    String result2 = data2.getData().getAttributes().getName();
                    Log.d(TAG, "onResponse: " + result2);

                    sp.edit().putString(CATEGORY_NAME, result2).apply();
                    String info;
                    info = sp.getString(CATEGORY_NAME, "");