如何从RectangularIViewObjSeter更改charchama_adapter类中的ImageView?
RectangularIViewObjSeter类中的setView方法是我编写代码块以更改回收者视图适配器中的图像视图的地方。值和资源都不为null或0。我认为问题在于在图像视图中设置了上下文。但是我不确定。
public class RectangularIViewObjSeter implements IViewObjSeter {
Context context;
ImageView imageView;
public RectangularIViewObjSeter(Context context) {
this.context = context;
}
@Override
public void setView(String Value, int Resource) {
imageView = ((Activity)context).findViewById(Resource);
Uri uri = Uri.parse(Value);
Picasso.get().load(uri).into(imageView);
}
}
我的回收站视图是这样的(PS在片段中)
public class charchama_adapter extends
RecyclerView.Adapter<charchama_adapter.MyViewHolder> {
View v;
ArrayList<ContentPOJO> list;
int sizeOfNesContentList;
int Height, Width;
Context context;
public charchama_adapter(ArrayList<ContentPOJO> list, Context context) {
this.list = new ArrayList<>();
this.list = list;
if (list.isEmpty()) {
sizeOfNesContentList = 0;
} else {
sizeOfNesContentList = list.size();
}
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public MyViewHolder(View itemView) {
super(itemView);
}
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int
viewType) {
v =
LayoutInflater.from(parent.getContext()).inflate(R.layout.charchama_adapter,
null);
Height = parent.getHeight();
Width = parent.getWidth();
context = parent.getContext();
MyViewHolder viewHolder = new MyViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
SetView setView = new SetView(context);
ViewResource viewResource = new
ViewResourceAdapter().setHeaderResource(R.id.contentHeader_adapterCharchama)
.setBodyResource(R.id.contentBody_adapterCharchama)
.setImageResource(R.id.backgroundImage_charchamaAdapter)
.setAuthorAndDateResource(R.id.autherAndDate_charchamaFragment)
.setNewsCategory(R.id.newsCategory_charchamaAdapter).getViewResource();
ContentPOJO contentPOJO = list.get(position);
setView.setResources(viewResource, contentPOJO, false);
}
@Override
public int getItemCount() {
return sizeOfNesContentList;
}
}
这是另一个设置我的观点的课程
public class SetView implements ISetView {
Context context;
public SetView(Context context) {
this.context = context;
}
@Override
public void setResources(ViewResource viewResource, ContentPOJO
contentPOJO, Boolean CircularImage) {
if(viewResource.getImageResource() !=0){
IViewObjSeter iViewObjSeter;
if (CircularImage == true) {
iViewObjSeter = new CircularIViewObjSeter(context);
} else {
iViewObjSeter = new RectangularIViewObjSeter(context);
}
iViewObjSeter.setView(contentPOJO.getImageURL(),
viewResource.getImageResource());
}
TextViewSetter textViewSetter = new TextViewSetter(context);
if (viewResource.getAuthorAndDateResource() != 0) {
if (contentPOJO.getDate().equals(null)) {
textViewSetter.setView(contentPOJO.getAuthor(),
viewResource.getAuthorAndDateResource());
} else {
textViewSetter.setView(contentPOJO.getAuthor() + " | " +
contentPOJO.getDate(), viewResource.getAuthorAndDateResource());
}
}
if(viewResource.getBodyResource()!=0){
textViewSetter.setView(contentPOJO.getStory(),
viewResource.getBodyResource());
}
if(viewResource.getHeaderResource() !=0){
textViewSetter.setView(contentPOJO.getHeading(),
viewResource.getHeaderResource());
}
if(viewResource.getNewsCategory()!=0){
textViewSetter.setView(contentPOJO.getNewsCategory(),
viewResource.getNewsCategory());
}
}
}
答案 0 :(得分:0)
在charchama_adapter
类中,您定义了一个名为context
的字段,如下所示:
Context context;
但是您没有在charchama_adapter
类的构造函数中初始化它,这就是问题。
public charchama_adapter(ArrayList<ContentPOJO> list, Context context) {
this.list = new ArrayList<>();
this.list = list;
this.context = context; //add this line
if (list.isEmpty()) {
sizeOfNesContentList = 0;
} else {
sizeOfNesContentList = list.size();
}
}
答案 1 :(得分:0)
好像我对我的问题有一个答案。大声笑 插入((Activity)上下文).findViewById()的view.findViewById()似乎效果很好。
public class RectangularIViewObjSeter implements IViewObjSeter {
Context context;
ImageView imageView;
public RectangularIViewObjSeter(Context context) {
this.context = context;
}
@Override
public void setView(View view, String Value, int Resource) {
imageView = view.findViewById(Resource);
Picasso.get().load(Value).into(imageView);
}
}