jeudi 29 octobre 2015

How to check a record is existed in database inside getItemViewType

I have a list of products, each row has a function "Save" which will store the item product to local database. I'm using SwipeMenuListView via http://ift.tt/1qk7zQb and i have to check record is existed or not in getItemViewType in ListAdapter to remove function Save of the row which saved. But the problem is, the "check record is existed or not" made my listview freeze after notifyDataSetChanged for a while (click "Save" then notifyDataSetChanged then list is freezed until i touch listview again). How should i fix it? Every suggestion will be highly appreciated. Thanks in advance. Here is my code: @Override public boolean onMenuItemClick(int position, SwipeMenu menu, int index) { switch (index) { case 0: break; case 1: ProductDetailsVo.ProductInfo productInfo = listResult.get(position); insertProductToDB(productInfo); break; }

and method insertProductToDB: private void insertProductToDB(ProductDetailsVo.ProductInfo productInfo) { databaseManager.insertProductToDB(productInfo); if (adapter != null) { adapter.notifyDataSetChanged(); } }

Adapter:

@Override
public int getViewTypeCount() {
   return 2;
}

@Override
public int getItemViewType(int position) {
   if (databaseManager.isProductSaved(listProds.get(position).getProductId())) {// cause listview freeze
      return 1;
   }
   return 0;
}

Aucun commentaire:

Enregistrer un commentaire