Manzoor Ahmad
Manzoor Ahmad

Reputation: 541

Populating RecyclerView with ListArray

I am populating my recycler view with the list array. I already tried the String Array to be it is not working out for me. am i doing it all wrong or do i have to use an arrayadapter?

if i had to go array adapter how could i do it? I already have populated listview with arrayadapter but as i am in a middle of ocean i don't want to try something big. the code for my recyclerview adapter is as follow.

it doesn't show an error. but the view is populating with the last item on array.

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();
}

}

Upvotes: 0

Views: 1160

Answers (2)

kkaun
kkaun

Reputation: 603

As addition to Pavneet_Singh's answer, you can also add bind() method to your custom ViewHolder and bind the data related to RecyclerView item in this method. I used this approach in simple projects when things became more complicated. It should work like this:

//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));
}

Edit: with proper usage this should help you to avoid problems with finding holder's bound properties, like textView etc. Additionally, in the future you can action holder's items listeners in the bind() method.

Upvotes: 0

Pavneet_Singh
Pavneet_Singh

Reputation: 37404

First: Use your customize ViewHolder inside AudioRecycleViewAdapter so change this

extends RecyclerView.Adapter<RecyclerView.ViewHolder>

to this

extends RecyclerView.Adapter<AudioRecycleViewAdapter.ViewHolder>

Second : you are going through all elements, just to display last value in textHeader and also use holder.txtHeader

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        holder.txtHeader.setText(values.get(position));

}

Third : Child views of item Layout view, should be inside ViewHolder so

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);
    }
}

Upvotes: 1

Related Questions