Я создаю RecyclerView, который показывает изображения и текстовое представление. Но Imageview выдает мне ошибку, когда я устанавливаю данные в классе адаптера, а затем выдает ошибку ниже:
Невозможно применить Imageview к TypedArray
Я также пытался использовать тип данных Integer, но он также дает ошибка. Пожалуйста, помогите выйти из этого.
Код адаптера рециркулятора здесь:
public class RecyclerAdpater extends RecyclerView.Adapter<RecyclerAdpater.ViewHolder> {
ArrayList<NewModel>newModels;
Context context;
private int limit = 4;
RecyclerInterface recyclerInterface;
public RecyclerAdpater(ArrayList<NewModel>newModels, Context context){
this.newModels = newModels;
this.context = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recy_layout, parent,false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.imageView.setImageResource(newModels.get(position).getSociallogo()); //here i am getting error.
holder.textView.setText(newModels.get(position).getSocailtext());
}
@Override
public int getItemCount() {
if(newModels.size() > limit){
return limit;
}else {
return newModels.size();
}
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView imageView;
TextView textView, viewtext;
public ViewHolder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.sociallogo);
textView = itemView.findViewById(R.id.socialtext);
imageView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent intent = new Intent(context, WebviewActivity.class);
intent.putExtra("URL NAME", newModels.get(getAdapterPosition()).getHref());
context.startActivity(intent);
}
}
Код фрагмента, когда я устанавливаю адаптер:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_for_you, container, false);
recyclerView = view.findViewById(R.id.recyclerview1);
TypedArray logimages = getResources().obtainTypedArray(R.array.social_image);
String[] logotext = getResources().getStringArray(R.array.social_name);
String [] href = getResources().getStringArray(R.array.social_url);
newModels = new ArrayList<>();
for(int i =0; i<logimages.length();i++) {
NewModel newModel = new NewModel(logimages, logotext[i], href[i]);
newModels.add(newModel);
}
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL,false);
readapter= new RecyclerAdpater(newModels,getContext());
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(readapter);
return view;
}
NewMode.class файл:
public class NewModel {
TypedArray sociallogo;
String socailtext;
String href;
public NewModel(TypedArray sociallogo, String socailtext, String href){
this.sociallogo = sociallogo;
this.socailtext= socailtext;
this.href = href;
}
public TypedArray getSociallogo(){
return sociallogo;
}
public String getSocailtext(){
return socailtext;
}
public String getHref(){
return href;
}
}