bindView switch-case 性能问题
bindView switch-case performace issues
我目前正在编写一个应用程序,需要根据我的数据库中的条目在 ListView
中设置图像。
我直接在 customCursorAdapter
的 bindView()
方法中通过 switch-case 实现了这一点。由于经常调用 bindView
,我的 ListView
开始卡顿,一切都变慢了。有什么方法可以 "outsource" 这个数据库比较?这是我的源代码:
public class customCursorAdapter extends CursorAdapter {
public customCursorAdapter(Context context, Cursor cursor){
super(context, cursor, 0);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.layout_listitems, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// Find fields to populate in inflated template
TextView brand = (TextView) view.findViewById(R.id.listBrand);
TextView model = (TextView) view.findViewById(R.id.listModel);
TextView lastMoved = (TextView) view.findViewById(R.id.dateContent);
TextView lastCharged = (TextView) view.findViewById(R.id.lastChargedContent);
ImageView brandPhoto = (ImageView) view.findViewById(R.id.brandImage);
// Extract properties from cursor
String brandList = cursor.getString(cursor.getColumnIndexOrThrow("Brand"));
String modelList = cursor.getString(cursor.getColumnIndexOrThrow("Model"));
String lastMovedList = cursor.getString(cursor.getColumnIndexOrThrow("LastMoved"));
String lastChargedList = cursor.getString(cursor.getColumnIndexOrThrow("LastCharge"));
switch(brandList){
case "Ferrari":
brandPhoto.setImageResource(R.drawable.ferrari);
break;
case "Porsche":
brandPhoto.setImageResource(R.drawable.porsche);
break;
case "VW":
case "Volkswagen":
brandPhoto.setImageResource(R.drawable.volkswagen);
break;
case "Mini":
brandPhoto.setImageResource(R.drawable.mini);
break;
case "Audi":
brandPhoto.setImageResource(R.drawable.audi);
break;
case "Aston Martin":
brandPhoto.setImageResource(R.drawable.aston);
break;
case "Alpina":
brandPhoto.setImageResource(R.drawable.alpina);
break;
case "Jeep":
brandPhoto.setImageResource(R.drawable.jeep);
break;
case "Bentley":
brandPhoto.setImageResource(R.drawable.bentley);
break;
case "Dodge":
brandPhoto.setImageResource(R.drawable.dodge);
break;
case "Fiat":
brandPhoto.setImageResource(R.drawable.fiat);
break;
case "Ford":
brandPhoto.setImageResource(R.drawable.ford);
break;
case "Lexus":
brandPhoto.setImageResource(R.drawable.lexus);
break;
case "Mercedes":
brandPhoto.setImageResource(R.drawable.mercedes);
break;
case "Maybach":
brandPhoto.setImageResource(R.drawable.maybach);
break;
case "Peugeot":
brandPhoto.setImageResource(R.drawable.peugeot);
break;
case "Skoda":
case "Škoda":
brandPhoto.setImageResource(R.drawable.skoda);
break;
case "Rolls Royce":
brandPhoto.setImageResource(R.drawable.rolls);
break;
case "Smart":
brandPhoto.setImageResource(R.drawable.smart);
break;
case "Opel":
brandPhoto.setImageResource(R.drawable.opel);
break;
case "Nissan":
brandPhoto.setImageResource(R.drawable.nissan);
break;
case "Alfa Romeo":
brandPhoto.setImageResource(R.drawable.alfa);
break;
case "BMW":
brandPhoto.setImageResource(R.drawable.bmw);
break;
case "Seat":
brandPhoto.setImageResource(R.drawable.seat);
break;
case "Lamborghini":
brandPhoto.setImageResource(R.drawable.lamborghini);
break;
default: brandPhoto.setImageResource(R.drawable.ic_directions_carhd);
}
// Populate fields with extracted properties
brand.setText(brandList);
model.setText(modelList);
lastMoved.setText(lastMovedList);
lastCharged.setText(lastChargedList);
}
}
我从你的代码中可以看出,品牌和与之相关的图像是一对一的映射。我不知道你为什么不把资源的 id 放在数据库中,这样你就不必在 switch
案例中传递 brandList
并在每次必须填充图像时进行比较在您的每个列表项中。
所以我强烈建议,将图像名称保存在您获取其他信息的同一个数据库中 table。这样您只需获取与品牌关联的图片名称,然后将其作为图片资源放入您的 ImageView
中。
因此您需要执行以下操作。首先准备一个包含图像资源 ID 的 HashMap
。
String[] images = {"ferrari", "alpina", "porsche", "volkswagen", .......};
Map<String, Integer> brandImagesMap = new HashMap<String, Integer>();
for(String imageName : images) {
int drawableResourceId = this.getResources().getIdentifier(imageName, "drawable", this.getPackageName());
brandImagesMap.put(imageName, drawableResourceId);
}
一旦您的地图在 customCursorAdapter
class 的构造函数中填充了图像的资源 ID,您就可以轻松地从地图中高效地获取图像资源 ID 并将其作为ImageView
的资源。请注意,我假设您将图像名称保存在数据库中的单独列中 table。
String photoResourceName = cursor.getString(cursor.getColumnIndexOrThrow("Photo"));
brandPhoto.setImageResource(brandImagesMap.get(photoResourceName));
代码未经测试。请根据您的要求进行修改。
更新
使用 Glide 在 RecyclerView
中加载图像。它实现起来非常简单,当然也很高效。
在您的 bindView
函数中,只需将加载图片的代码替换为以下代码即可。
Glide.with(context)
.load(brandImagesMap.get(photoCol))
.fitCenter()
.placeholder(R.drawable.ic_directions_carhd)
.into(myHolder.brandPhoto);
想了解更多Glide的使用方法,请关注this link。
我目前正在编写一个应用程序,需要根据我的数据库中的条目在 ListView
中设置图像。
我直接在 customCursorAdapter
的 bindView()
方法中通过 switch-case 实现了这一点。由于经常调用 bindView
,我的 ListView
开始卡顿,一切都变慢了。有什么方法可以 "outsource" 这个数据库比较?这是我的源代码:
public class customCursorAdapter extends CursorAdapter {
public customCursorAdapter(Context context, Cursor cursor){
super(context, cursor, 0);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.layout_listitems, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// Find fields to populate in inflated template
TextView brand = (TextView) view.findViewById(R.id.listBrand);
TextView model = (TextView) view.findViewById(R.id.listModel);
TextView lastMoved = (TextView) view.findViewById(R.id.dateContent);
TextView lastCharged = (TextView) view.findViewById(R.id.lastChargedContent);
ImageView brandPhoto = (ImageView) view.findViewById(R.id.brandImage);
// Extract properties from cursor
String brandList = cursor.getString(cursor.getColumnIndexOrThrow("Brand"));
String modelList = cursor.getString(cursor.getColumnIndexOrThrow("Model"));
String lastMovedList = cursor.getString(cursor.getColumnIndexOrThrow("LastMoved"));
String lastChargedList = cursor.getString(cursor.getColumnIndexOrThrow("LastCharge"));
switch(brandList){
case "Ferrari":
brandPhoto.setImageResource(R.drawable.ferrari);
break;
case "Porsche":
brandPhoto.setImageResource(R.drawable.porsche);
break;
case "VW":
case "Volkswagen":
brandPhoto.setImageResource(R.drawable.volkswagen);
break;
case "Mini":
brandPhoto.setImageResource(R.drawable.mini);
break;
case "Audi":
brandPhoto.setImageResource(R.drawable.audi);
break;
case "Aston Martin":
brandPhoto.setImageResource(R.drawable.aston);
break;
case "Alpina":
brandPhoto.setImageResource(R.drawable.alpina);
break;
case "Jeep":
brandPhoto.setImageResource(R.drawable.jeep);
break;
case "Bentley":
brandPhoto.setImageResource(R.drawable.bentley);
break;
case "Dodge":
brandPhoto.setImageResource(R.drawable.dodge);
break;
case "Fiat":
brandPhoto.setImageResource(R.drawable.fiat);
break;
case "Ford":
brandPhoto.setImageResource(R.drawable.ford);
break;
case "Lexus":
brandPhoto.setImageResource(R.drawable.lexus);
break;
case "Mercedes":
brandPhoto.setImageResource(R.drawable.mercedes);
break;
case "Maybach":
brandPhoto.setImageResource(R.drawable.maybach);
break;
case "Peugeot":
brandPhoto.setImageResource(R.drawable.peugeot);
break;
case "Skoda":
case "Škoda":
brandPhoto.setImageResource(R.drawable.skoda);
break;
case "Rolls Royce":
brandPhoto.setImageResource(R.drawable.rolls);
break;
case "Smart":
brandPhoto.setImageResource(R.drawable.smart);
break;
case "Opel":
brandPhoto.setImageResource(R.drawable.opel);
break;
case "Nissan":
brandPhoto.setImageResource(R.drawable.nissan);
break;
case "Alfa Romeo":
brandPhoto.setImageResource(R.drawable.alfa);
break;
case "BMW":
brandPhoto.setImageResource(R.drawable.bmw);
break;
case "Seat":
brandPhoto.setImageResource(R.drawable.seat);
break;
case "Lamborghini":
brandPhoto.setImageResource(R.drawable.lamborghini);
break;
default: brandPhoto.setImageResource(R.drawable.ic_directions_carhd);
}
// Populate fields with extracted properties
brand.setText(brandList);
model.setText(modelList);
lastMoved.setText(lastMovedList);
lastCharged.setText(lastChargedList);
}
}
我从你的代码中可以看出,品牌和与之相关的图像是一对一的映射。我不知道你为什么不把资源的 id 放在数据库中,这样你就不必在 switch
案例中传递 brandList
并在每次必须填充图像时进行比较在您的每个列表项中。
所以我强烈建议,将图像名称保存在您获取其他信息的同一个数据库中 table。这样您只需获取与品牌关联的图片名称,然后将其作为图片资源放入您的 ImageView
中。
因此您需要执行以下操作。首先准备一个包含图像资源 ID 的 HashMap
。
String[] images = {"ferrari", "alpina", "porsche", "volkswagen", .......};
Map<String, Integer> brandImagesMap = new HashMap<String, Integer>();
for(String imageName : images) {
int drawableResourceId = this.getResources().getIdentifier(imageName, "drawable", this.getPackageName());
brandImagesMap.put(imageName, drawableResourceId);
}
一旦您的地图在 customCursorAdapter
class 的构造函数中填充了图像的资源 ID,您就可以轻松地从地图中高效地获取图像资源 ID 并将其作为ImageView
的资源。请注意,我假设您将图像名称保存在数据库中的单独列中 table。
String photoResourceName = cursor.getString(cursor.getColumnIndexOrThrow("Photo"));
brandPhoto.setImageResource(brandImagesMap.get(photoResourceName));
代码未经测试。请根据您的要求进行修改。
更新
使用 Glide 在 RecyclerView
中加载图像。它实现起来非常简单,当然也很高效。
在您的 bindView
函数中,只需将加载图片的代码替换为以下代码即可。
Glide.with(context)
.load(brandImagesMap.get(photoCol))
.fitCenter()
.placeholder(R.drawable.ic_directions_carhd)
.into(myHolder.brandPhoto);
想了解更多Glide的使用方法,请关注this link。