Fanatic Falcon
Fanatic Falcon

Reputation: 1

Unable to display all the lst in an arraylist

I have an array of 4 list items and I am trying to display all of them on my recycler view but for some reason only the last list is being displayed.

class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> {

    private static final String TAG = "MainAdapter";

    List<String> mStrings[];
    int k=0;
    int len;


    public MainAdapter(List<String> mStrings[]) {
        len = mStrings.length;
        this.mStrings = new List[len];
        for(int i=0;i<mStrings.length;i++) {
            this.mStrings[i] = mStrings[i];
        }
    }

    @NonNull
    @Override
    public MainAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.display_view,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MainAdapter.ViewHolder holder, int position) {
        Log.d(TAG,"Positions from onBind are:" +mStrings);
        holder.mItemName.setText(mStrings[k].get(position));
    }


    @Override
    public int getItemCount() {
        return mStrings[k].size();

    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        public TextView mItemName;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            mItemName = itemView.findViewById(R.id.item_name);
        }
     }
}

In the above code I have a list array called mStrings[] that consist of 4 list values:

So each array has a list of item

[Pack 1, Pack5, Pack 8] [Pack34, Pack 82, Pack 12] [Pack 90, Pack 12, Pack982] [Pack 111, Pack 233, Pack100]

When I am trying to display all the list I am only able to display the last one. I know I am doing something wrong with the code but I am unable to figure out how I can display all 4 of them in the recycler view. Any suggestions or help?

Upvotes: 0

Views: 32

Answers (1)

Ankur tyagi
Ankur tyagi

Reputation: 31

Problem is in the loop:

this.mStrings[i] = mStrings[i];

you need to do it like:

this.mStrings[this.mStrings.length+i] = mStrings[i]; 

Upvotes: 3

Related Questions