简体   繁体   English

如何在Android / java中使用回收视图和毕加索显示图像?

[英]How to show images using recycle view and picasso in android/ java?

I am having trouble showing images that I collected from a website into a recycle view. 我无法将从网站收集的图像显示到回收视图中。 I first got the image urls using jsoup. 我首先使用jsoup获得了图像URL。 Then I added the urls in a list called imgURLS. 然后,将URL添加到名为imgURLS的列表中。 Now I want to show those images in gridview that has recycle view. 现在,我想在具有回收视图的gridview中显示这些图像。 The code I have no errors but the images are not showen when I run my android project. 我没有错误的代码,但是当我运行我的android项目时图像没有显示。 Its just blank. 它只是空白。 Here is some of my code 这是我的一些代码

MAINACTIVITY.JAVA MAINACTIVITY.JAVA

   @Override
protected void onCreate(Bundle savedInstanceState) {
    setTheme(R.style.AppTheme);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
        new URLcollector().execute();
    imageView = findViewById(R.id.imageView);
    //setBKG = findViewById(R.id.setBKG);
    rvMain = findViewById(R.id.rvMain);

    // can add another parameter here like text
    MyAdapter adapter = new MyAdapter(imgURLS);
    rvMain.setLayoutManager(new GridLayoutManager(MainActivity.this, 2));
    rvMain.setAdapter(adapter);

}

then still in mainactivity.java 然后仍然在mainactivity.java中

public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {

    // String[] companyList;
    List<String> logoList;
    ImageView currentView;
    Bitmap imgBitmaps;

    public MyAdapter( List<String> logoList) {
        // this.companyList = companyList;
        this.logoList = logoList;
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item, parent, false);
        MyViewHolder viewHolder = new MyViewHolder(v);
        return viewHolder;
    }

     @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        // load image from picasso and then

        Context context = holder.logo.getContext(); //<----- Add this line

        Picasso.with(context).load(imgURLS.get(position)).into(holder.logo);


    }

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

My questions are 我的问题是

1) Am I using Picasso correctly? 1)我是否正确使用毕加索?

2) Why did I not be able to see the images(keep in mind visible is on in xml)? 2)为什么我看不到图像(请注意xml中的可见标记)?

Let me know if I need to provide more of my code. 让我知道是否需要提供更多代码。

@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
    // load image from picasso and then pass it to set Image
    Picasso.with(MainActivity.this)
            .load(logoList.get(position))
            .placeholder(R.mipmap.ic_launcher) // optional
            .error(R.mipmap.ic_launcher) //if error
            .into(holder.logo);
    holder.logo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "This is: " , Toast.LENGTH_SHORT).show();
        }
    });
    // holder.name.setText(companyList[position]);
}

If your url is correct. 如果您的网址正确。 try something like this and don't forget to set INTERNT permission in Manifest. 尝试这样的事情,不要忘记在清单中设置INTERNT权限。

<uses-permission android:name="android.permission.INTERNET"/>

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

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