Java Can';t使用毕加索在RecyclerView中加载图像
我正在使用Volley和毕加索库在一个RecyclerView中显示一个包含3个文本视图的ImageView。 下面是row.xml的布局设计 以下是输出: 图像未显示在RecyclerView中,上面是row.xml的快照和输出。您可以将输出与row.xml文件进行比较。如有任何帮助,将不胜感激,谢谢 row.xmlJava Can';t使用毕加索在RecyclerView中加载图像,java,android,json,android-recyclerview,android-volley,Java,Android,Json,Android Recyclerview,Android Volley,我正在使用Volley和毕加索库在一个RecyclerView中显示一个包含3个文本视图的ImageView。 下面是row.xml的布局设计 以下是输出: 图像未显示在RecyclerView中,上面是row.xml的快照和输出。您可以将输出与row.xml文件进行比较。如有任何帮助,将不胜感激,谢谢 row.xml <android.support.v7.widget.CardView android:layout_width="match_parent" andr
<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>
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);
}
}
}
公共类MyAdapter扩展了RecyclerView.Adapter
{
私人语境;
私有ArrayList模型;
公共MyAdapter(ArrayList模型、上下文应用程序上下文)
{
this.context=上下文;
这个。模型=模型;
}
@非空
@凌驾
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup父级,int-viewType)
{
视图v=LayoutInflater.from(parent.getContext()).flate(R.layout.row,null);
返回新的MyViewHolder(v);
}
@凌驾
public void onBindViewHolder(@NonNull MyViewHolder,int位置)
{
holder.tvUsername.setText(models.get(position.getUsername());
holder.tvUserid.setText(models.get(position.getUserid());
holder.tvUserurl.setText(models.get(position.getUserprofileurl());
毕加索
.load(models.get(position.getUserimage())
.into(holder.imgAvatar);
}
@凌驾
public int getItemCount()
{
返回models.size();
}
公共类MyViewHolder扩展了RecyclerView.ViewHolder
{
ImageView imgAvatar;
TextView tvUsername、tvUserid、tvUserurl;
公共MyViewHolder(查看项目视图)
{
超级(项目视图);
imgAvatar=itemView.findviewbyd(R.id.id\u化身);
tvUsername=itemView.findViewById(R.id.tvnamehere);
tvUserid=itemView.findviewbyd(R.id.id\u userid);
tvUserurl=itemView.findViewById(R.id.id\u url);
}
}
}
当您为
RecyclerView
分配适配器时,它仍然为空,您必须颠倒调用顺序:
mAdapter = new MyAdapter(models,getApplicationContext());
recyclerView.setAdapter(mAdapter);
因此,
mAdapter
将具有在setAdapter
中使用的价值,请检查MyAdapter的构造函数
public MyAdapter(ArrayList<Model> models, Context applicationContext)
{
this.context = context; // '= context;' should be '= applicationContext;'
this.models = models;
}
公共MyAdapter(ArrayList模型、上下文应用程序上下文)
{
this.context=context;//'=context;'应该是'=applicationContext;'
这个。模型=模型;
}
应该是
public MyAdapter(ArrayList<Model> models, Context applicationContext)
{
this.context = applicationContext;
this.models = models;
}
公共MyAdapter(ArrayList模型、上下文应用程序上下文)
{
this.context=applicationContext;
这个。模型=模型;
}
谢谢!那真的帮了我的忙。。但是输出不是我期望的。请检查更新后的帖子。这是另一个stackoverflow主题的问题,您的加载代码似乎是对的,您只是在tableLayout上错过了这个问题。您使用的是android:layout\u width=“match\u parent”,而它应该是android:layout\u width=“0dp”再加上一个标签,将所有可用空间都放到左边的android:layout_weight=“1”
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;
}
***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