imageView使用错误的可绘制资源

时间:2018-04-14 21:52:14

标签: java android android-layout imageview

我在其上设置图像时,我的imageViews使用了错误的资源。我清除了缓存,清理了项目并重新启动了Android Studio。似乎什么都行不通...... 有时imageView会显示corectly,而有时它会使用与我设置的资源完全不同的资源。

我正在使用卡布局btw。

这是我的活动

public class Tab1 extends Fragment {
Button button = null;
ListView listView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.tab1, container, false);
    listView = rootView.findViewById(R.id.listey);
    listView.setDivider(null);
    listView.setDividerHeight(0);
    Intent intent = getActivity().getIntent();
    Log.e("poop", Arrays.toString(intent.getStringArrayExtra("Values")));
    final String[] info = intent.getStringArrayExtra("Values");
    ArrayList<Card> list = new ArrayList<>();

    list.add(new Card("drawable://" + R.drawable.victory, "Wins: " + String.valueOf(info[0])));
    list.add(new Card("drawable://" + R.drawable.image2, "Kills: " + String.valueOf(info[1])));
    list.add(new Card("drawable://" + R.drawable.image3, "Matches: " + String.valueOf(info[2])));
    list.add(new Card("drawable://" + R.drawable.image5, "K/D: " + String.valueOf(info[3])));
    list.add(new Card("drawable://" + R.drawable.image3, "Average Match Time: " + String.valueOf(4)));

    CustomListAdapter adapter = new CustomListAdapter(getActivity(), R.layout.card_layout, list);
    listView.setAdapter(adapter);




    return rootView;
}
}

这是我的适配器类。

public class CustomListAdapter  extends ArrayAdapter<Card> {

private static final String TAG = "CustomListAdapter";

private Context mContext;
private int mResource;
private int lastPosition = -1;

/**
 * Holds variables in a View
 */
private static class ViewHolder {
    TextView title;
    ImageView image;
}

/**
 * Default constructor for the PersonListAdapter
 * @param context
 * @param resource
 * @param objects
 */
public CustomListAdapter(Context context, int resource, ArrayList<Card> objects) {
    super(context, resource, objects);
    mContext = context;
    mResource = resource;
    //sets up the image loader library
    setupImageLoader();

}

@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {


    //get the persons information
    String title = getItem(position).getTitle();
    String imgUrl = getItem(position).getImgURL();


    try{


        //create the view result for showing the animation
        final View result;

        //ViewHolder object
        ViewHolder holder;

        if(convertView == null){
            LayoutInflater inflater = LayoutInflater.from(mContext);
            convertView = inflater.inflate(mResource, parent, false);
            holder= new ViewHolder();
            holder.title = (TextView) convertView.findViewById(R.id.cardTitle);
            holder.image = (ImageView) convertView.findViewById(R.id.cardImage);

            result = convertView;

            convertView.setTag(holder);
        }
        else{
            holder = (ViewHolder) convertView.getTag();
            result = convertView;
        }


        lastPosition = position;

        holder.title.setText(title);



        //create the imageloader object
        ImageLoader imageLoader = ImageLoader.getInstance();

        int defaultImage = mContext.getResources().getIdentifier("@drawable/image_failed",null,mContext.getPackageName());

        //create display options
        DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true)
                .cacheOnDisc(true).resetViewBeforeLoading(true)
                .showImageForEmptyUri(defaultImage)
                .showImageOnFail(defaultImage)
                .showImageOnLoading(defaultImage).build();

        //download and display image from url
        imageLoader.displayImage(imgUrl, holder.image, options);

        return convertView;
    }catch (IllegalArgumentException e){
        Log.e(TAG, "getView: IllegalArgumentException: " + e.getMessage() );
        return convertView;
    }

}

/**
 * Required for setting up the Universal Image loader Library
 */
private void setupImageLoader(){
    // UNIVERSAL IMAGE LOADER SETUP
    DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
            .cacheOnDisc(true).cacheInMemory(true)
            .imageScaleType(ImageScaleType.EXACTLY)
            .displayer(new FadeInBitmapDisplayer(300)).build();

    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
            mContext)
            .defaultDisplayImageOptions(defaultOptions)
            .memoryCache(new WeakMemoryCache())
            .discCacheSize(100 * 1024 * 1024).build();

    ImageLoader.getInstance().init(config);
    // END - UNIVERSAL IMAGE LOADER SETUP
}
}`

1 个答案:

答案 0 :(得分:0)

知道了。如果有人使用相同的customListAdapter类,则需要从.memoryCache(new WeakMemoryCache()) .discCacheSize(100 * 1024 * 1024方法中删除行setupImageLoader。然后,图像将以正确的顺序显示