我已在我的应用中实施了RecyclerView,并在Recycler View上设置了onClickListener。文本字段已成功传递,但我在recyclerview中也有一个图像,该图像通过毕加索传递。所以我尝试检索存储在另一个类中的图像网址,但我得到一个空值。
代码是:
package xyz.xyz;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.media.Image;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.squareup.picasso.Picasso;
import org.w3c.dom.Text;
import java.util.ArrayList;
import java.util.List;
public class FlatSearchCardAdapter extends RecyclerView.Adapter<FlatSearchCardAdapter.ViewHolder> {
private Context context;
private ImageLoader imageLoader;
List<FlatCardItemReturn> flatlistCard;
public FlatSearchCardAdapter(List<FlatCardItemReturn> flatlistCard, Context context){
super();
//Getting all the superheroes
this.flatlistCard = flatlistCard;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.flatrenter_list_item, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
FlatCardItemReturn flatcarditemreturn = flatlistCard.get(position);
Picasso.with(context).load(flatcarditemreturn.getImageUrl()).placeholder(R.drawable.loadingpicasso).error(R.drawable.errorpicasso).resize(100, 100).into(holder.imageView);
holder.textViewId.setText(flatcarditemreturn.getId());
holder.textViewType.setText(flatcarditemreturn.getType());
holder.textViewFurnishing.setText(flatcarditemreturn.getFurnishing());
holder.textViewArea.setText(flatcarditemreturn.getArea());
holder.textViewPrice.setText(flatcarditemreturn.getPrice());
holder.textViewNumberofrooms.setText(flatcarditemreturn.getNumberofrooms());
}
@Override
public int getItemCount() {
return flatlistCard.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder{
public ImageView imageView;
public TextView textViewType;
public TextView textViewPrice;
public TextView textViewFurnishing;
public TextView textViewArea;
public TextView textViewNumberofrooms;
public TextView textViewId;
public FlatCardItemReturn flatcarditemreturn = new FlatCardItemReturn();
public ViewHolder(View itemView) {
super(itemView);
textViewId = (TextView) itemView.findViewById(R.id.id);
imageView = (ImageView) itemView.findViewById(R.id.flatphoto);
textViewType = (TextView) itemView.findViewById(R.id.type);
textViewFurnishing = (TextView) itemView.findViewById(R.id.furnishing);
textViewArea = (TextView) itemView.findViewById(R.id.area);
textViewPrice = (TextView) itemView.findViewById(R.id.price);
textViewNumberofrooms = (TextView) itemView.findViewById(R.id.numberofrooms);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(v.getContext(), flatrenterpostview.class);
i.putExtra("idvalue", textViewId.getText().toString());
i.putExtra("typevalue", textViewType.getText().toString());
i.putExtra("furnishingvalue", textViewFurnishing.getText().toString());
i.putExtra("areavalue", textViewArea.getText().toString());
i.putExtra("pricevalue", textViewPrice.getText().toString());
i.putExtra("numberofroomsvalue", textViewNumberofrooms.getText().toString());
i.putExtra("imageurl", flatcarditemreturn.getImageUrl());
v.getContext().startActivity(i);
}
});
}
}
}
我对如何设置位置非常困惑,以便获得当前值。如果有人能帮助我,将不胜感激。 为了说清楚,i.putExtra(&#34; imageurl&#34;,flatcarditemreturn.getImageUrl());什么都没有。我希望将url存储在我的班级中。网址存储得很完美,我查了一下。
答案 0 :(得分:1)
为了说清楚,
i.putExtra("imageurl", flatcarditemreturn.getImageUrl());
什么都没有。我希望我的班级存储的网址
那么为什么要在FlatCardItemReturn
中创建新的ViewHolder
?这一行:
public FlatCardItemReturn flatcarditemreturn = new FlatCardItemReturn();
您希望将正确的数据项设置为ViewHolder
,而不是实例化一个新的“空”&#39;一。您可以通过向ViewHolder
添加方法轻松完成此操作:
public void setData(FlatCardItemReturn item) {
flatcarditemreturn = item;
}
现在,只需从onBindViewHolder()
:
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
FlatCardItemReturn flatcarditemreturn = flatlistCard.get(position);
// other stuff omitted
holder.setData(flatcarditemreturn);
}
您现在也可以更改:
public FlatCardItemReturn flatcarditemreturn = new FlatCardItemReturn();
成:
public FlatCardItemReturn flatcarditemreturn;