里面getItemViewType是如何检查的记录存在于数据库 [英] How to check a record is existed in database inside getItemViewType

查看:164
本文介绍了里面getItemViewType是如何检查的记录存在于数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个产品列表,每一行都有一个功能保存,将项目产品存储到本地数据库中。我通过 https://github.com/baoyongzhang/SwipeMenuListView 使用SwipeMenuListView和我有检查记录在getItemViewType在ListAdapter是存在还是不删除保存功能保存它的行。
但问题是,在检查记录存在与否notifyDataSetChanged后使我的ListView冻结一段时间(点击保存,然后再notifyDataSetChanged名单冻结,直到我再次触摸列表视图)。
我应该怎么解决?
每一个建议将是非常美联社preciated。提前致谢。这里是我的code:

  @覆盖
公共布尔onMenuItemClick(INT位置,SwipeMenu菜单,INT指数){
   开关(指数){
   情况下0:
      打破;
   情况1:
   ProductDetailsVo.ProductInfo productInfo = listResult.get(位置);
   insertProductToDB(productInfo);
   打破;
}

和方法insertProductToDB:

 私人无效insertProductToDB(ProductDetailsVo.ProductInfo productInfo){
   databaseManager.insertProductToDB(productInfo);
   如果(适配器!= NULL){
   adapter.notifyDataSetChanged();
   }
}

适配器:

  @覆盖
公众诠释getViewTypeCount(){
   返回2;
}@覆盖
公众诠释getItemViewType(INT位置){
   如果(databaseManager.isProductSaved(listProds.get(位置).getProductId())){//原因冻结的ListView
      返回1;
   }
   返回0;
}


解决方案

于是冻结可能是由于众多的来电 isProductSaved ,你可以通过评论肯定这一点他们和测试,或通过与Android设备进行监控分析你的应用程序。

您可以很容易地通过添加一个字段存在为您的产品删除的数据库访问。默认值是 insertProductToDB 将它,如果插入成功设置为true。随着你的 getViewType 会快很多。

  @覆盖
公众诠释getItemViewType(INT位置){
   如果(listProds.get(位置).isExists())){
      返回1;
   }
   返回0;
}

如果你不想改变一个额外的领域模型对象,你也能保持在适配器映射像 SparseArray 。它将包含 ID 保存的产品。

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 https://github.com/baoyongzhang/SwipeMenuListView 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;
}

解决方案

So the freezes are probably due the numerous calls to isProductSaved, you can be sure of that by commenting them and test, or by profiling your app with the Android Device Monitor.

You can easily remove that database access by adding a field exists to your products. The default value would be false and insertProductToDB would set it to true if the insertion succeeds. With that your getViewType will be much faster.

@Override
public int getItemViewType(int position) {
   if (listProds.get(position).isExists())) {
      return 1;
   }
   return 0;
}

If you don't want to alter your model object with an extra field, you can also maintain a map like a SparseArray in the adapter. It would contain the id of the saved products.

这篇关于里面getItemViewType是如何检查的记录存在于数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆