【问题标题】:Can't load images in RecyclerView using Picasso无法使用 Picasso 在 RecyclerView 中加载图像
【发布时间】:2018-07-30 21:55:26
【问题描述】:

我正在使用 Volley 和 Picasso 库在 RecyclerView 中显示带有 3 个 TextView 的 ImageView。 下面是row.xml的布局设计

row.xml preview

下面是输出:

preview output

RecyclerView 中没有显示图像,上面是 row.xml 和输出的快照。您可以将输出与 row.xml 文件进行比较。任何帮助将不胜感激,谢谢!

行.xml

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardElevation="5dp"
    app:cardBackgroundColor="#fff"
    app:cardUseCompatPadding="true"
    app:contentPadding="5dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/id_avatar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher"/>
        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp">

            <TableRow>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Name :"
                    android:textSize="18dp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/tvnamehere"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:text="Name here..."
                    android:textSize="18dp"
                    android:textStyle="bold"/>
            </TableRow>

            <TableRow>
                <TextView
                    android:id="@+id/id_userid"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="ID :"
                    android:textSize="18dp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/tvidhere"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:text="id here..."
                    android:textSize="18dp"
                    android:textStyle="bold" />
            </TableRow>

            <TableRow>
                <TextView
                    android:id="@+id/id_url"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Url :"
                    android:textSize="18dp"
                    android:textStyle="bold"/>

                <TextView
                    android:id="@+id/tvurlhere"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:text="url here..."
                    android:textSize="18dp"
                    android:textStyle="bold" />
            </TableRow>
        </TableLayout>
    </LinearLayout>
</android.support.v7.widget.CardView>

MainActivity.java

public class MainActivity extends AppCompatActivity
{
    public static final String URL = "https://api.myjson.com/bins/hm03k";
    RecyclerView recyclerView;
    MyAdapter mAdapter;
    ArrayList<Model> models = new ArrayList<>();

    @Override
    public void onResume()
    {
        super.onResume();
        retrieveData();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = (RecyclerView) findViewById(R.id.id_recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
    }

    public void retrieveData()
    {
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Loading...");
        progressDialog.show();

        StringRequest request = new StringRequest(URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response)
            {
                progressDialog.dismiss();
                try {
                    JSONArray jsonArray = new JSONArray(response);
                    for (int i = 0; i<jsonArray.length(); i++)
                    {
                        JSONObject object = jsonArray.getJSONObject(i);
                        Model model = new Model(object.getString("login"),
                                object.getString("id"),
                                object.getString("avatar_url"),
                                object.getString("url"));
                        models.add(model);
                    }
                    recyclerView.setAdapter(mAdapter);
                    mAdapter = new MyAdapter(models,getApplicationContext());
                }
                catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(MainActivity.this, "Error!", Toast.LENGTH_SHORT).show();
            }
        });

        RequestQueue queue = Volley.newRequestQueue(this);
        queue.add(request);
    }
}

Model.java(POJO 类)

public class Model
{
    String username,userid,userprofileurl,userimage;

    public Model(String username, String userid, String userprofileurl, String userimage) {
        this.username = username;
        this.userid = userid;
        this.userprofileurl = userprofileurl;
        this.userimage = userimage;
    }


    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getUserid() {
        return userid;
    }

    public void setUserid(String userid) {
        this.userid = userid;
    }

    public String getUserprofileurl() {
        return userprofileurl;
    }

    public void setUserprofileurl(String userprofileurl) {
        this.userprofileurl = userprofileurl;
    }

    public String getUserimage() {
        return userimage;
    }

    public void setUserimage(String userimage) {
        this.userimage = userimage;
    }
}

MyAdapter.class

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>
{
    private Context context;
    private ArrayList<Model> models;

    public MyAdapter(ArrayList<Model> models, Context applicationContext)
    {
        this.context = context;
        this.models = models;
    }

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

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position)
    {
        holder.tvUsername.setText(models.get(position).getUsername());
        holder.tvUserid.setText(models.get(position).getUserid());
        holder.tvUserurl.setText(models.get(position).getUserprofileurl());

        Picasso.get()
                .load(models.get(position).getUserimage())
                .into(holder.imgAvatar);

    }

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

    public class MyViewHolder extends RecyclerView.ViewHolder
    {
        ImageView imgAvatar;
        TextView tvUsername,tvUserid,tvUserurl;

        public MyViewHolder(View itemView)
        {
            super(itemView);
            imgAvatar = itemView.findViewById(R.id.id_avatar);
            tvUsername = itemView.findViewById(R.id.tvnamehere);
            tvUserid = itemView.findViewById(R.id.id_userid);
            tvUserurl = itemView.findViewById(R.id.id_url);
        }
    }
}

【问题讨论】:

    标签: java android json android-recyclerview android-volley


    【解决方案1】:
        ***Try This :***
    
        Dependancy : 
        implementation "com.squareup.picasso:picasso:2.4.0"
    
        Adapter :
    
              Picasso.with(context)
                        .load(new File(food.getImage()))
                        .error(R.drawable.ic_home_black_24dp)
                        .resize(50,50)
                        .placeholder(R.drawable.ic_home_black_24dp)
                        .into(holder.imageView);
    
    load : your model class 
    into : your imageview
    

    【讨论】:

      【解决方案2】:

      检查您的 MyAdapter 的构造函数

      public MyAdapter(ArrayList<Model> models, Context applicationContext)
      {
          this.context = context; // '= context;' should be '= applicationContext;'
          this.models = models;
      }
      

      应该是

       public MyAdapter(ArrayList<Model> models, Context applicationContext)
      {
          this.context = applicationContext;
          this.models = models;
      }
      

      【讨论】:

        【解决方案3】:

        当您为 RecyclerView 分配适配器时,它仍然为空,您必须颠倒调用顺序:

                    mAdapter = new MyAdapter(models,getApplicationContext());
                    recyclerView.setAdapter(mAdapter);
        

        所以mAdapter 将具有在setAdapter 使用的价值。

        【讨论】:

        • 谢谢!这真的帮助了我..但是输出不是我所期望的..请检查更新的帖子。
        • 这是另一个stackoverflow主题的问题,您的加载代码正确,您只是在tableLayout上错过了这个,您使用android:layout_width =“match_parent”,而它应该是android:layout_width =“0dp”和一个更多标签将所有可用空间放在左侧 android:layout_weight="1"
        猜你喜欢
        • 1970-01-01
        • 2020-01-09
        • 2019-08-14
        • 1970-01-01
        • 2020-07-11
        • 2019-08-20
        • 2016-12-18
        • 2020-03-18
        • 1970-01-01
        相关资源
        最近更新 更多