简体   繁体   English

将ArrayList添加到recyclerView

[英]Adding ArrayList to recyclerView

I'm trying to add an ArrayList to my RecyclerView. 我正在尝试将ArrayList添加到我的RecyclerView中。 The problem is that it's not setting up anything, only blank spaces are present. 问题在于它没有设置任何内容,只有空白。

The code for my mainActivity is 我的mainActivity的代码是

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 我的Adapter类的代码是

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. 问题在于代码未在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. 在我添加这一行之后...。它再次仅显示第一个textView而不是完整的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.... 我尝试为XML文件的水平布局添加tge代码,但没有使其成为水平...。

You're passing the entire ArrayList Instead of the actual size. 您正在传递整个ArrayList而不是实际大小。 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: SizeConstructor构造SizeConstructor更改为此:

public SizeConstructor(ArrayList<String> sizeArray) {

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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