简体   繁体   English

图像未加载到 ImageView - Android

[英]Image is not loading in ImageView - Android

I want to load the clicked image in the next activity in fullscreen view.我想在全屏视图的下一个活动中加载单击的图像。 I don't know what I am doing wrong.我不知道我做错了什么。 The app is running completely fine but the image is not loading on the next screen.该应用程序运行良好,但图像未在下一个屏幕上加载。 am new to android development and I thought I should start learning by doing a project.我是 android 开发的新手,我想我应该从做一个项目开始学习。 I am not understanding what is the problem in my coding.我不明白我的编码有什么问题。

public class CategoriesAdapter extends RecyclerView.Adapter<CategoriesAdapter.ImageViewHolder> {
   private Context mCtx;
   private List<Category> imageslist;

    public CategoriesAdapter(Context mCtx, List<Category> imageslist) {
        this.mCtx = mCtx;
        this.imageslist = imageslist;
    }


    @NonNull
    @Override
    public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(mCtx).inflate(R.layout.recyclerview_images,parent,false);
   return new ImageViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ImageViewHolder holder, final int position) {
        final Category images=imageslist.get(position);
        Glide.with(mCtx).load(images.url).into(holder.imageView);
        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mCtx, LoadWall.class);
                intent.putExtra("url", (Parcelable) imageslist.get(position));
                mCtx.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return imageslist.size();
    }

    class ImageViewHolder extends RecyclerView.ViewHolder{
       ImageView imageView;

        public ImageViewHolder(@NonNull View itemView) {

            super(itemView);
            imageView=itemView.findViewById(R.id.image_view);

        }
    }

}

This is the activity where I want to load the image.这是我要加载图像的活动。 But its not loading.但它没有加载。 Please Help请帮忙

public class LoadWall extends AppCompatActivity {
ImageView imageView;
int myImage;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_load_wall);
        imageView=findViewById(R.id.load_image);
        get();

    }
    private void get(){
        if(getIntent().hasExtra("url")){
            String image=getIntent().getStringExtra("url");
            set(image);
        }
        else{
            Toast.makeText(this,"No Data",Toast.LENGTH_SHORT).show();
        }
    }
private void set(String image){
    Glide.with(this).load(image).into(imageView);
}
}

Here you only need to pass URL as a String.这里只需要将 URL 作为字符串传递。 Parcelable is not required. Parcelable 不是必需的。

intent.putExtra("url", imageslist.get(position).getUrl());

Option 1)选项1)

In your next activity you are looking for String.在您的下一个活动中,您正在寻找字符串。

String image=getIntent().getStringExtra("url");

in first activity in onClick() change your code to this.在 onClick() 的第一个活动中,将您的代码更改为此。

Intent intent=new Intent(mCtx, LoadWall.class);
intent.putExtra("url", imageslist.get(position)); 
intent.startActivity(intent);

As i see imageslist.get(position) will return Category so you will need to add your url parameter.如我所见, imageslist.get(position) 将返回类别,因此您需要添加 url 参数。 Something like this imageslist.get(position).getUrl();像这样的 imageslist.get(position).getUrl();

Option 2)选项 2)

If you want to get Category object then in next activity change to this如果您想获得类别 object 然后在下一个活动中更改为这个

Category category = getIntent().getParcelableExtra("url");

Your imagesList contains data of type Category and you send an entry from this list to the next activity.您的imagesList包含Category类型的数据,并且您将此列表中的条目发送到下一个活动。 However, the second activity expects a String to be received.但是,第二个活动期望收到一个String It will check to see if it has an extra (it has - but not what you need), then try to retrieve that value as a String which will result in a null String.它将检查它是否有额外的(它有 - 但不是你需要的),然后尝试将该值作为String检索,这将导致null字符串。 Having null , Glide will not load anything.拥有null ,Glide 不会加载任何东西。

In other words, you made a small mistake on what data you use.换句话说,您在使用的数据上犯了一个小错误。 When you load the data, you do it correctly by using the url field加载数据时,使用url字段正确执行

final Category images=imageslist.get(position);
Glide.with(mCtx).load(**images.url**).into(holder.imageView);

But when you are passing it to the next activity, you send the entire object但是当你将它传递给下一个活动时,你发送整个 object

intent.putExtra("url", **(Parcelable) imageslist.get(position)**);

Using the url field as you did in the first case will make your app work properly.在第一种情况下使用url字段将使您的应用程序正常工作。

Start your Activity开始您的Activity

 Intent intent=new Intent(mCtx, LoadWall.class);
 intent.putExtra("url", images.url);
 mCtx.startActivity(intent);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM