简体   繁体   中英

Adding ArrayList to recyclerView

I'm trying to add an ArrayList to my RecyclerView. The problem is that it's not setting up anything, only blank spaces are present.

The code for my mainActivity is

ArrayList<String> Size = new ArrayList<>(Arrays.asList(SizeArrayFinal));

rec = (RecyclerView)findViewById(R.id.SizeRecycler);
rec.setHasFixedSize(true);
rec.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
sizeConstructorList = new ArrayList<>();

for (int i = 0; i <= Size.size(); i++){
   SizeConstructor sizeConstructor = new SizeConstructor(Size);
   sizeConstructorList.add(sizeConstructor);
}

recAdapter = new SizeAdapter(sizeConstructorList,getApplicationContext());
rec.setAdapter(recAdapter);

The code for my Adapter class is

public class SizeAdapter extends RecyclerView.Adapter<SizeAdapter.ViewHolder> {

    private List<SizeConstructor> sizeConstructorList;
    private Context context;

    public SizeAdapter(List<SizeConstructor> sizeConstructorList, Context context) {
        this.sizeConstructorList = sizeConstructorList;
        this.context = context;
    }

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

    @Override
    public void onBindViewHolder(SizeAdapter.ViewHolder holder, int position) {
        SizeConstructor sizeConstructor = sizeConstructorList.get(position);
        holder.button1.setText(sizeConstructor.getSize());
    }

    @Override
    public int getItemCount() {
        return sizeConstructorList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        public TextView button1;

        public ViewHolder(View itemView) {
            super(itemView);

            button1 = (TextView)itemView.findViewById(R.id.SizeButton);
        }
    }
}

And finally my constructor class is

public class SizeConstructor {

    String Size;

    public SizeConstructor(ArrayList<String> sizeArray) {
    }

    public SizeConstructor(String size) {
        Size = size;
    }

    public String getSize() {
        return Size;
    }
}

The issue is that the code is not setting the values in the recyclerView.

Please refer to the image below to see the issue. 这是参考图片

Moreover there is a huge Gap below which is not required as i was setting the orientation to be horizontal. Any help would be great as I have to submit this project really soon.

RecycleView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false);

rec.setLayoutManager(layoutManager);

After i added this line....it is again showing just the first textView instead of complete arrayList.

I wanted to make the layout horizontal. I tried to add tge code for horizontal layout im XML file but it wasn't making it horizontal....

You're passing the entire ArrayList Instead of the actual size. So change this line:

SizeConstructor sizeConstructor = new SizeConstructor(Size);

to

SizeConstructor sizeConstructor = new SizeConstructor(Size.get(indexValue));

So finally to this:

 for (int i = 0; i <= Size.size(); i++){

     SizeConstructor sizeConstructor = new SizeConstructor(Size.get(i));
     sizeConstructorList.add(sizeConstructor);
 }

Hope this helps!

change this code

 for (int i = 0; i <= Size.size(); i++){

            SizeConstructor sizeConstructor = new SizeConstructor(Size);
            sizeConstructorList.add(sizeConstructor);
        }

to

for (int i = 0; i <= Size.size(); i++){

            SizeConstructor sizeConstructor = new SizeConstructor(Size.get(i));
            sizeConstructorList.add(sizeConstructor);
        }

in SizeConstructor change constructor to this:

public SizeConstructor(ArrayList<String> sizeArray) {

  Size = String.valueOf(sizeArray != null ? sizeArray.size() : 0);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM