Hi everyone, my adapter isn't updating after calling AsyncTask!
I am using Fast Android Networking Library, what I am doing is :
Make a get request to the server to get the latest update version then comparing it to the version stored the my shared preferences if there is an update I make another get request to get the new data, then I clear my database and insert the new data, in the final I get the data from database and I give it to the adapter.
The problem is that in the first try the recycled view is empty, but when I hit the return button then I go back to the activity I find that the recycler view is updated.
I've tried many things but nothing seems to work so please if you could help you are the most welcome, here'is my code :
public class ProductsListActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private static ProgressBar progressBar;
static JSONObject productJsonObject;
static JSONArray productPackages;
static JSONObject packageJsonObject;
static Product product;
static Package mpackage;
static String name;
static String description;
static String destination;
static String dose;
static String storage;
static String image;
static String packageUid;
static String packageName;
static String packageUnit;
static String packageTaste;
static String packagePrice;
static String packageLifetime;
static String packagePalletization;
static ProductsListAdapter productsListAdapter;
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_products_list);
progressBar = findViewById(R.id.productsListProgressbar);
recyclerView = findViewById(R.id.productslist);
ProductsListAyncTask ayncTask = new ProductsListAyncTask();
ayncTask.execute();
Log.d("rani fe", "More constructuer");
}
private class ProductsListAyncTask extends AsyncTask<Object, Object, List<Product>> {
@override
protected void onPreExecute() {
super.onPreExecute();
Log.d("rani fe", "preexcute");
progressBar.setVisibility(View.VISIBLE);
}
@override
protected List<Product> doInBackground(Object[] objects) {
Log.d("rani fe", "Do in Background");
updateData();
List<Product> products = Product.getAllProducts();
Log.d("rani fe", "kharja te do in background");
return products;
}
@override
protected void onPostExecute(List<Product> products) {
super.onPostExecute(products);
Log.d("rani fe", "Post execute");
progressBar.setProgress(0);
progressBar.setVisibility(View.GONE);
recyclerView.setVisibility(View.VISIBLE);
recyclerView.setHasFixedSize(true);
productsListAdapter = new ProductsListAdapter(getApplicationContext(), products);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
recyclerView.setAdapter(productsListAdapter);
Log.d("rani fe", "kharja te postexcute");
}
}
public void getProductsFromServer() {
AndroidNetworking.initialize(getApplicationContext());
Log.d("network", "network");
AndroidNetworking.get(URLs.products)
.addHeaders("Authorization", "Token " + SharedPreferencesManager.getInstance(getApplicationContext()).getUserToken()) // posting jso.setTag("test")
.setTag("PRODUCTS")
.setPriority(Priority.HIGH)
.build()
.getAsJSONArray(new JSONArrayRequestListener() {
@override
public void onResponse(JSONArray response) {
//progressBar.setVisibility(View.VISIBLE);
Log.d("response", response.toString());
Log.d("rani fe", "on response te3 getproducts from server");
// Deleting All Records From Product and Package Tables
new Delete().from(Product.class).execute();
new Delete().from(Package.class).execute();
//Product(String name, String description, String destination, String dose, String storage, String image, String language)
for (int i = 0; i < response.length(); i++) {
try {
productJsonObject = response.getJSONObject(i);
Log.d("prodname", productJsonObject.getString("name"));
Log.d("prodname", productJsonObject.getString("image"));
name = productJsonObject.getString("name");
description = productJsonObject.getString("description");
destination = productJsonObject.getString("destination");
dose = productJsonObject.getString("dose");
storage = productJsonObject.getString("storage");
image = productJsonObject.getString("image");
product = new Product(name,
description,
destination,
dose,
storage,
image,
"français");
product.save();
productPackages = response.getJSONObject(i).getJSONArray("products_package");
Log.d("lenght", Integer.toString(productPackages.length()));
for (int j = 0; j < productPackages.length(); j++) {
packageJsonObject = productPackages.getJSONObject(j);
packageUid = packageJsonObject.getString("uid");
packageName = packageJsonObject.getString("package_type");
Log.d("packname", packageName);
packageUnit = packageJsonObject.getString("unit");
packageTaste = packageJsonObject.getString("taste");
packagePrice = packageJsonObject.getString("price");
packageLifetime = packageJsonObject.getString("lifetime");
packagePalletization = packageJsonObject.getString("palletization");
//(String name, String product, String unit, String taste, String price, String lifetime, String palletization)
mpackage = new Package(packageUid, packageName, product.getName(), packageUnit, packageTaste, packagePrice, packageLifetime, packagePalletization);
mpackage.save();
}
} catch (JSONException e) {
e.printStackTrace();
}
Log.d("rani fe", "Kbel constructuer");
}
}
@override
public void onError(ANError anError) {
}
});
}
public void updateData() {
final boolean[] updated = new boolean[1];
AndroidNetworking.initialize(getApplicationContext());
AndroidNetworking.get(URLs.latestUpdate)
.addHeaders("Authorization", "Token " + SharedPreferencesManager.getInstance(getApplicationContext()).getUserToken()) // posting jso.setTag("test")
.setPriority(Priority.HIGH)
.build()
.getAsJSONObject(new JSONObjectRequestListener() {
@override
public void onResponse(JSONObject response) {
Log.d("rani fe", "on response te3 update data");
// do anything with response
try {
String serverLastestUpdate = response.getString("latest_update");
Log.d("serveurlatestupdate", serverLastestUpdate);
Log.d("shareupdate", SharedPreferencesManager.getInstance(getApplicationContext()).getLatestUpdate());
Log.d("checupdate", Boolean.toString(AromplusManager.checkForUpdate(getApplicationContext(), serverLastestUpdate)));
updated[0] = AromplusManager.checkForUpdate(getApplicationContext(), serverLastestUpdate);
if (updated[0]) {
getProductsFromServer();
SharedPreferencesManager.getInstance(getApplicationContext()).setLatestUpdate(serverLastestUpdate);
Log.d("source", "Loading from serveur");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@override
public void onError(ANError error) {
// handle error
Log.d("erro", error.toString());
}
});
}
}
I am using Fast Android Networking Library, what I am doing is :
Make a get request to the server to get the latest update version then comparing it to the version stored the my shared preferences if there is an update I make another get request to get the new data, then I clear my database and insert the new data, in the final I get the data from database and I give it to the adapter.
The problem is that in the first try the recycled view is empty, but when I hit the return button then I go back to the activity I find that the recycler view is updated.
I've tried many things but nothing seems to work so please if you could help you are the most welcome, here'is my code :
public class ProductsListActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private static ProgressBar progressBar;
static JSONObject productJsonObject;
static JSONArray productPackages;
static JSONObject packageJsonObject;
static Product product;
static Package mpackage;
static String name;
static String description;
static String destination;
static String dose;
static String storage;
static String image;
static String packageUid;
static String packageName;
static String packageUnit;
static String packageTaste;
static String packagePrice;
static String packageLifetime;
static String packagePalletization;
static ProductsListAdapter productsListAdapter;
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_products_list);
progressBar = findViewById(R.id.productsListProgressbar);
recyclerView = findViewById(R.id.productslist);
ProductsListAyncTask ayncTask = new ProductsListAyncTask();
ayncTask.execute();
Log.d("rani fe", "More constructuer");
}
private class ProductsListAyncTask extends AsyncTask<Object, Object, List<Product>> {
@override
protected void onPreExecute() {
super.onPreExecute();
Log.d("rani fe", "preexcute");
progressBar.setVisibility(View.VISIBLE);
}
@override
protected List<Product> doInBackground(Object[] objects) {
Log.d("rani fe", "Do in Background");
updateData();
List<Product> products = Product.getAllProducts();
Log.d("rani fe", "kharja te do in background");
return products;
}
@override
protected void onPostExecute(List<Product> products) {
super.onPostExecute(products);
Log.d("rani fe", "Post execute");
progressBar.setProgress(0);
progressBar.setVisibility(View.GONE);
recyclerView.setVisibility(View.VISIBLE);
recyclerView.setHasFixedSize(true);
productsListAdapter = new ProductsListAdapter(getApplicationContext(), products);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
recyclerView.setAdapter(productsListAdapter);
Log.d("rani fe", "kharja te postexcute");
}
}
public void getProductsFromServer() {
AndroidNetworking.initialize(getApplicationContext());
Log.d("network", "network");
AndroidNetworking.get(URLs.products)
.addHeaders("Authorization", "Token " + SharedPreferencesManager.getInstance(getApplicationContext()).getUserToken()) // posting jso.setTag("test")
.setTag("PRODUCTS")
.setPriority(Priority.HIGH)
.build()
.getAsJSONArray(new JSONArrayRequestListener() {
@override
public void onResponse(JSONArray response) {
//progressBar.setVisibility(View.VISIBLE);
Log.d("response", response.toString());
Log.d("rani fe", "on response te3 getproducts from server");
// Deleting All Records From Product and Package Tables
new Delete().from(Product.class).execute();
new Delete().from(Package.class).execute();
//Product(String name, String description, String destination, String dose, String storage, String image, String language)
for (int i = 0; i < response.length(); i++) {
try {
productJsonObject = response.getJSONObject(i);
Log.d("prodname", productJsonObject.getString("name"));
Log.d("prodname", productJsonObject.getString("image"));
name = productJsonObject.getString("name");
description = productJsonObject.getString("description");
destination = productJsonObject.getString("destination");
dose = productJsonObject.getString("dose");
storage = productJsonObject.getString("storage");
image = productJsonObject.getString("image");
product = new Product(name,
description,
destination,
dose,
storage,
image,
"français");
product.save();
productPackages = response.getJSONObject(i).getJSONArray("products_package");
Log.d("lenght", Integer.toString(productPackages.length()));
for (int j = 0; j < productPackages.length(); j++) {
packageJsonObject = productPackages.getJSONObject(j);
packageUid = packageJsonObject.getString("uid");
packageName = packageJsonObject.getString("package_type");
Log.d("packname", packageName);
packageUnit = packageJsonObject.getString("unit");
packageTaste = packageJsonObject.getString("taste");
packagePrice = packageJsonObject.getString("price");
packageLifetime = packageJsonObject.getString("lifetime");
packagePalletization = packageJsonObject.getString("palletization");
//(String name, String product, String unit, String taste, String price, String lifetime, String palletization)
mpackage = new Package(packageUid, packageName, product.getName(), packageUnit, packageTaste, packagePrice, packageLifetime, packagePalletization);
mpackage.save();
}
} catch (JSONException e) {
e.printStackTrace();
}
Log.d("rani fe", "Kbel constructuer");
}
}
@override
public void onError(ANError anError) {
}
});
}
public void updateData() {
final boolean[] updated = new boolean[1];
AndroidNetworking.initialize(getApplicationContext());
AndroidNetworking.get(URLs.latestUpdate)
.addHeaders("Authorization", "Token " + SharedPreferencesManager.getInstance(getApplicationContext()).getUserToken()) // posting jso.setTag("test")
.setPriority(Priority.HIGH)
.build()
.getAsJSONObject(new JSONObjectRequestListener() {
@override
public void onResponse(JSONObject response) {
Log.d("rani fe", "on response te3 update data");
// do anything with response
try {
String serverLastestUpdate = response.getString("latest_update");
Log.d("serveurlatestupdate", serverLastestUpdate);
Log.d("shareupdate", SharedPreferencesManager.getInstance(getApplicationContext()).getLatestUpdate());
Log.d("checupdate", Boolean.toString(AromplusManager.checkForUpdate(getApplicationContext(), serverLastestUpdate)));
updated[0] = AromplusManager.checkForUpdate(getApplicationContext(), serverLastestUpdate);
if (updated[0]) {
getProductsFromServer();
SharedPreferencesManager.getInstance(getApplicationContext()).setLatestUpdate(serverLastestUpdate);
Log.d("source", "Loading from serveur");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@override
public void onError(ANError error) {
// handle error
Log.d("erro", error.toString());
}
});
}
}