gpt4 book ai didi

java - bindView switch-case 性能问题

转载 作者:太空宇宙 更新时间:2023-11-04 10:38:06 24 4
gpt4 key购买 nike

我目前正在编写一个应用程序,需要根据数据库中的条目在 ListView 中设置图像。

我直接在 customCursorAdapterbindView() 方法中通过 switch-case 实现了这一点。由于 bindView 被频繁调用,我的 ListView 开始出现卡顿,一切都变得缓慢。有没有办法“外包”这个数据库比较?这是我的源代码:

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 并在每次必须在每个列表项中填充图像时进行比较。

因此,我强烈建议将图像名称保留在与获取其他信息的同一数据库表中。这样您只需获取与品牌关联的图像名称,然后将其作为图像资源放入您的 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);
}

一旦您的 map 在 customCursorAdapter 类的构造函数中填充了图像的资源 ID,您就可以轻松高效地从 map 中获取图像资源 ID,并将其作为 ImageView 的资源。请注意,我假设您将图像的名称保留在数据库表中的单独列中。

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

关于java - bindView switch-case 性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49284997/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com