So, I am trying to load some data from my local server to my Listview.
My Fragment's onCreateView
has:
myAdapter = new myRecyclerViewAdapter(Products.ITEMS, mListener);
In my Products
Class, I have:
static {
loadItems(0);
}
public static void addItem(Product item) {
ITEMS.add(item);
ITEM_MAP.put(item.id, item);
}
public static void loadItems(int start){
Log.d(TAG, String.valueOf(ITEMS.size()));
ProductsService.fetchAll(start, new ServiceListCallback() {
@Override
public void onDone(ArrayList baseModelList) {
Log.d(TAG,"Done");
Iterator<Product> iterator = baseModelList.iterator();
while (iterator.hasNext()) {
addItem(iterator.next());
}
}
});
}
ProductService
has:
public static ArrayList<Product> fetchAll(int start, final ServiceListCallback serviceListCallback){
productList = new ArrayList<Product>();
HttpAgent.get("http://192.168.0.100/products")
.goJsonArray(new JsonArrayCallback() {
@Override
protected void onDone(boolean success, JSONArray jsonArray) {
//Log.d(TAG, String.valueOf(success));
if(success){
for(int i = 0; i < jsonArray.length() ; i++){
JSONObject jsonObject = jsonArray.optJSONObject(i);
Product singleProduct = new Product(
jsonObject.optString("id"),
jsonObject.optString("title"),
jsonObject.optString("details"),
jsonObject.optString("description"),
jsonObject.optDouble("price"),
jsonObject.optString("thumbnail")
);
productList.add(singleProduct);
}
Log.d(TAG, String.valueOf(productList.size()));
}
serviceListCallback.onDone(productList);
}
});
return productList;
}
The flow works perfectly but the listview is not updated.
I know that I am supposed to call myAdapter.notifyItemInserted(Products.ITEMS.size());
But I cant get the myAdapter instance from the callback. Whats the work around?
I am still new to java and android, so code improvements are highly appreciated.
Aucun commentaire:
Enregistrer un commentaire