在加载图像之前单击listview时出现异常

时间:2016-05-12 08:47:09

标签: android listview onitemclicklistener android-glide onitemclick

有一个自定义列表视图,可显示图像和文本。我正在使用Glide图像库来加载图像。因此,当我在listview中搜索图像和文本时,但在图像加载之前,如果我点击它,则会看到Java Null Pointer异常。如何避免在图像加载之前进行单击,或者需要在itemclick监听器上进行任何更改。

项目单击:

java.util.Stack

我正在检查是否有图像url然后在使用Glide加载后获取图像,但是在我加载图像之前单击时出现异常。有什么建议吗?

答案:

在适配器传递显示活动中创建Onclicklistener。

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    descusers du = dusers.get(position);

    final String Limage = du.image;


    if (!Limage.isEmpty()) {
    final ImageView imageView1 = (ImageView) view.findViewById(R.id.imageList);
    final GlideBitmapDrawable bitmapDrawable = (GlideBitmapDrawable) imageView1.getDrawable();
    final Bitmap yourBitmap = bitmapDrawable.getBitmap();

    Dialog builder = new Dialog(this);
    builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
    builder.getWindow().setBackgroundDrawable(
            new ColorDrawable(android.graphics.Color.TRANSPARENT));
    builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialogInterface) {
            //nothing;
        }
    });

    ImageView imageView = new ImageView(this);
    imageView.setImageBitmap(yourBitmap);
    p=new PhotoViewAttacher(imageView);


    builder.addContentView(imageView, new RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));
    builder.show();

    }
}

适配器构造函数:

viewholder.iview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Dialog builder = new Dialog(listdisplay);
                builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
                builder.getWindow().setBackgroundDrawable(
                        new ColorDrawable(android.graphics.Color.TRANSPARENT));
                builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialogInterface) {
                        //nothing;
                    }
                });
                ImageView imageView = new ImageView(listdisplay);

                final GlideBitmapDrawable bitmapDrawable = (GlideBitmapDrawable) finalViewholder.iview.getDrawable();
                final Bitmap yourBitmap = bitmapDrawable.getBitmap();
                imageView.setImageBitmap(yourBitmap);
                p=new PhotoViewAttacher(imageView);


                builder.addContentView(imageView, new RelativeLayout.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT));
                builder.show();
            }
        });

2 个答案:

答案 0 :(得分:0)

在getView()

上尝试使用此代码
@Override public View getView(int position, View convertView, ViewGroup parent) {
final T item = getItem(position);
....
Glide.with(getContext())
    .load(UIUtils.getThumbnailImageUrl(item.images))
    .placeholder(R.drawable.default_thumbnail_picture)
    .crossFade()
    .listener(new RequestListener<String, GlideDrawable>() {
        @Override
        public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
            return false;
        }

        @Override
        public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
            return false;
        }
    })
    .into(viewHolder.mExerciseImageView);
return convertView;
}

这可能对你有所帮助。

答案 1 :(得分:0)

您可以检查图片是否为null。可能会在你正在使用的库中有回调(我不确定)

final Bitmap yourBitmap = bitmapDrawable.getBitmap();
if(yourBitmap==null){
 image.setEnabled(false);
}

因此,如果未加载图像,则可以避免单击。 或者您可以默认setEnabled(false),加载图片时使用setEnabled(true)