如何在android / java中使用循环视图和picasso显示图像?

时间:2018-05-31 14:05:51

标签: java android android-recyclerview picasso

我无法将从网站收集的图像显示为回收视图。我首先使用jsoup获取图像网址。然后我在名为imgURLS的列表中添加了url。现在我想在gridview中显示具有循环视图的那些图像。代码我没有错误但是当我运行我的android项目时图像没有显示。它只是空白。这是我的一些代码

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);

}

然后仍然在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();
    }
}

我的问题是

1)我正确使用毕加索吗?

2)为什么我无法看到图像(请记住xml中是否可见)?

如果我需要提供更多代码,请告诉我。

1 个答案:

答案 0 :(得分:1)

@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]);
}

如果你的网址是正确的。尝试这样的事情,不要忘记在Manifest中设置INTERNT权限。

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