使用ListArray填充RecyclerView

我正在使用列表数组填充我的回收站视图。我已经试过字符串数组,因为它不适合我。我做这一切都错了,或者我必须使用arrayadapter?使用ListArray填充RecyclerView

如果我不得不去阵列适配器我怎么能做到这一点?我已经用arrayadapter填充了listview,但是因为我在海洋中间,我不想尝试一些大的东西。我的recyclerview适配器的代码如下。

它不显示错误。但视图正在填充数组上的最后一个项目。

public class AudioRecycleViewAdapter extends 

RecyclerView.Adapter<RecyclerView.ViewHolder> {

private List<String> values = Arrays.asList ("Al Feel","Al Humazah","Al

Kausar","Al Maoon","Al Quresh","Dr Israr",

"Al Kafiroon","An Nasr","Al Lahab","Al Ikhlaas","Al Falaq","An

Naas","Iman Part 1",

"Iman Part 2","Iman Part 3","Iman Part 4","Iman Part 5");

Context context;

InterstitialAd mInterstitialAd;

private int audiolayout;

public TextView txtHeader;

public TextView txtFooter;

TextView txtarabic;

public AudioRecycleViewAdapter(List<String> values, int audiolayout, Context context, InterstitialAd intAdView) throws IOException {

this.audiolayout = audiolayout;

this.context = context;

this.mInterstitialAd = intAdView;

}

public Context getContext() {

context = this.getContext();

return context;

}

// Provide a reference to the views for each data item

// Complex data items may need more than one view per item, and

// you provide access to all the views for a data item in a view holder

public class ViewHolder extends RecyclerView.ViewHolder {

// each data item is just a string in this case

public View layout;

public ViewHolder(View v) {

super(v);

layout = v;

txtHeader = (TextView) v.findViewById(R.id.audio_subtitle);

txtFooter = (TextView) v.findViewById(R.id.audio_title);

txtarabic = (TextView) v.findViewById(R.id.arabic_title);

}

}

public void add(int position, String item) {

values.add(position, item);

notifyItemInserted(position);

}

public void remove(int position) {

values.remove(position);

notifyItemRemoved(position);

}

// Create new views (invoked by the layout manager)

@Override

public AudioRecycleViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,

int viewType) {

// create a new view

LayoutInflater inflater = LayoutInflater.from(

parent.getContext());

View v =

inflater.inflate(R.layout.audio_row_layout, parent, false);

// set the view's size, margins, paddings and layout parameters

ViewHolder vh = new ViewHolder(v);

return vh;

}

@Override

public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

int x;

for(position =0; position < values.size(); position++)

{

txtHeader.setText(values.get(position));

position++;

}

}

// Return the size of your dataset (invoked by the layout manager)

@Override

public int getItemCount() {

return values.size();

}

}

回答:

首先:用你的自定义ViewHolder内AudioRecycleViewAdapter所以改变这种

extends RecyclerView.Adapter<RecyclerView.ViewHolder> 

这个

extends RecyclerView.Adapter<AudioRecycleViewAdapter.ViewHolder> 

二:你正在经历的所有元素,只是为了显示textHeader最后一个值也可用holder.txtHeader

@Override 

public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

holder.txtHeader.setText(values.get(position));

}

三:项目布局视图的儿童的意见,应该是里面ViewHolder所以

public class ViewHolder extends RecyclerView.ViewHolder { 

// each data item is just a string in this case

public View layout;

public TextView txtHeader;

public TextView txtFooter;

public TextView txtarabic;

public ViewHolder(View v) {

super(v);

layout = v;

txtHeader = (TextView) v.findViewById(R.id.audio_subtitle);

txtFooter = (TextView) v.findViewById(R.id.audio_title);

txtarabic = (TextView) v.findViewById(R.id.arabic_title);

}

}

回答:

至于除了Pavneet_Singh的回答,您还可以添加bind()方法来定制ViewHolder并结合相关RecyclerView项数据在这个方法中。当事情变得更加复杂时,我在简单的项目中使用这种方法。它应该是这样的:

//adapter's inner 

class SomeClassViewHolder(val view: View) extends RecyclerView.ViewHolder(view) {

//here can be view initializing, like

txtHeader = (TextView) view.findViewById(R.id.audio_subtitle);

void bind(int position){

// all view binding logic goes here, for example:

txtHeader.setText("someText");

}

}

//and then in adapter

@Override

public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

holder.bind(values.get(position));

}

编辑:与正确使用这个应该可以帮助您避免问题找到holder的绑定属性,像textView等。此外,在未来,你可以行动持有者的项目听众在bind()方法中。

以上是 使用ListArray填充RecyclerView 的全部内容, 来源链接: utcz.com/qa/265408.html

回到顶部