将序列号添加到recyclerview

时间:2018-03-28 10:19:47

标签: android android-recyclerview

我正在尝试将序列号添加到android recyclelerview中的textview。但它会引发以下错误

我的代码在这里

for a in volume:
    a.get_text()

6 个答案:

答案 0 :(得分:5)

而不是

@Override
    public void onBindViewHolder(final RViewHolder holder,final int position) {
        final commdtylist listitem=listitems2.get(position);
        for(i=1;i<=listitems2.size();i++){
            holder.comm_code.setText(i);
        }
        }

使用

@Override
    public void onBindViewHolder(final RViewHolder holder,final int position) {
        final commdtylist listitem=listitems2.get(position);
            holder.comm_code.setText(String.valueOf(position+1));
      }

答案 1 :(得分:3)

使用此

 holder.comm_code.setText(String.valueOf(position+1));

答案 2 :(得分:1)

setText();方法要求 String 值作为参数而非 int

使用此

holder.comm_code.setText(i+"");

或者这个

 holder.comm_code.setText(String.valueOf(i));

而不是这个

holder.comm_code.setText(i);

修改

   holder.comm_code.setText(String.valueOf(position+1));

答案 3 :(得分:1)

试试这个::

  @Override
    public void onBindViewHolder(final RViewHolder holder,final int position) {
        final commdtylist listitem=listitems2.get(position);
        String str = "";
        for(i=1;i<=listitems2.size();i++){
            str = TextUtils.concat(str,String.valueOf(i)," ").toString();
        }
        holder.comm_code.setText(str);
    }

答案 4 :(得分:1)

他们在循环添加int i = 1中是错误的。

@Override
        public void onBindViewHolder(final RViewHolder holder,final int position) {
            final commdtylist listitem=listitems2.get(position);
            for(int i=1;i<=listitems2.size();i++){
                holder.comm_code.setText(""+position);
            }

答案 5 :(得分:0)

以上所有答案都是正确的,但是当您上下滚动时,顶视图和底视图的编号会发生变化,因为回收视图会回收屏幕上不存在的视图。 要解决该问题,只需编写:

//代码在Kotlin中

val number = (position+1)%(itemList.size+1)
    holder.comm_code.text = number.toString()

itemList 是您将在 recyclerAdapter 类中传递的列表。 基本上,您的位置在增加,但最终答案将始终小于等于您的列表大小。