我是android编码的初学者...我迷迷糊糊地遇到了这个问题,其中UIL在列表视图中显示了错误项目的错误图像,这是我的适配器:
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
public class ClassAdapter extends ArrayAdapter<String> {
public final String[] CarI;
public static class ViewHolder {
TextView carTitle;
ImageView carImage;
}
public ClassAdapter(Context context, String[] mclaren, String[] CarI) {
super(context, R.layout.custom_listview, mclaren);
this.CarI = CarI;
}
public ViewHolder holder;
public View getView(int position, View convertView, ViewGroup parent) {
SetupImageLoader();
final View result;
String singleCar = getItem(position);
if (convertView == null) {
LayoutInflater mcInflater = LayoutInflater.from(getContext());
convertView = mcInflater.inflate(R.layout.custom_listview, parent, false);
holder = new ViewHolder();
holder.carImage = convertView.findViewById(R.id.imageView);
holder.carTitle = convertView.findViewById(R.id.titleView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ImageLoader imageLoader = ImageLoader.getInstance();
DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheOnDisk(true).cacheInMemory(true)
.resetViewBeforeLoading(true).build();
try {
imageLoader.displayImage(CarI[position], holder.carImage, options);
} catch (Exception e) {
}
holder.carTitle.setText(singleCar);
return convertView;
}
public void SetupImageLoader() {
// UNIVERSAL IMAGE LOADER SETUP
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.cacheOnDisk(true)
.build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
getContext())
.defaultDisplayImageOptions(defaultOptions)
.memoryCache(new WeakMemoryCache())
.discCacheSize(100 * 1024 * 1024).build();
ImageLoader.getInstance().init(config);
// END - UNIVERSAL IMAGE LOADER SETUP
}
}
这也是具有列表视图的活动:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
public class Honda extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_honda);
String honda[]= {"Accord","Civic", "Civic Si", "Civic Type R","Clarity","CR-V","Fit","HR-V","Insight","Odyssey","Pilot","Ridgeline","Acura ILX","Acura MDX","Acura NSX","Acura RDX","Acura RLX","Acura TLX"};
final String hondaI[] = {
"drawable://" + R.drawable.accord,
"drawable://" + R.drawable.civic,
"drawable://" + R.drawable.civicsi,
"drawable://" + R.drawable.civictyper,
"drawable://" + R.drawable.clarity,
"drawable://" + R.drawable.crv,
"drawable://" + R.drawable.fit,
"drawable://" + R.drawable.hrv,
"drawable://" + R.drawable.insight,
"drawable://" + R.drawable.odyssey,
"drawable://" + R.drawable.hopilot,
"drawable://" + R.drawable.hridgeline,
"drawable://" + R.drawable.a124,
"drawable://" + R.drawable.mdx,
"drawable://" + R.drawable.nsx,
"drawable://" + R.drawable.rdx,
"drawable://" + R.drawable.a123,
"drawable://" + R.drawable.tlx,
};
LinearLayout ll = (LinearLayout) findViewById(R.id.layout);
final ListView list = (ListView) findViewById(R.id.list);
ListAdapter customAdapter = new ClassAdapter(this, honda, hondaI);
list.setAdapter(customAdapter);
}
}
这是XML代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Honda">
<ListView
android:id="@+id/list5"
android:layout_width="395dp"
android:layout_height="643dp"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp" />
</LinearLayout>
重要的是要提到我添加了多个列表视图,这些视图必须从drawable文件夹中加载图像,而这些图像是以错误的顺序甚至有时以错误的列表显示的!