从 CardViews 获取数据时为空

Null when getting data from CardViews

我正在尝试从 RecyclerView 中的 CardView 中的 EditText 获取信息。 RecyclerView按时间只显示4个CardView,总共有32个CardView。但是当我尝试获取未显示的 CardView 时,我得到的是 null 而不是 View。所以,我的问题是,如何从 RecyclerView 中获取所有 View

我创建了一个 RecyclerView,然后我使用自定义适配器用 CardView 填充它,并使用我从 HID 连接获得的信息。

private void readAll() throws Exception {
    mUsbCommunication.clearBuffer();
    byte[] s=new byte[]{115};
    mUsbCommunication.sendCmd(s,1);
    Thread.sleep(100);
    byte[] response=mUsbCommunication.getResponse();
    mUsbCommunication.clearBuffer();
    String validation=Integer.toHexString(response[4] & 0xFF);
    if(validation.equals("1")) {
        items.clear();
        for (int i = 0; i < 32; i++) {
            items.add(new Sector(readSector(returnSector(i)),"SECTOR "+String.valueOf(i),"","",String.valueOf(i)));
        }
        recycler = (RecyclerView) findViewById(R.id.reciclador);
        recycler.setHasFixedSize(true);

        lManager = new LinearLayoutManager(this);
        recycler.setLayoutManager(lManager);

        adapter = new CardViewAdapter(items);
        recycler.setAdapter(adapter);
    }
    else
        Toast.makeText(LecturaEscritura.this,"ERROR AUTHENTICATING",Toast.LENGTH_LONG).show();
}

CardView 中,我放置了一个 TextView,其中包含从连接中获取的文本,用户将对其进行编辑。用户编辑文本后,我从每个 CardView 中的 TextView 中获取文本,按位置获取 View 并拆分获取的文本。

CardViewAdapter cva=(CardViewAdapter)recycler.getAdapter();
LinkedList<Integer> positions=cva.getPositions();
if(posiciones!=null) {
      Log.d("LIST",String.valueOf(positions.size()));
      CardView v = (CardView) recycler.getLayoutManager().findViewByPosition(positions.get(i));
       if (v != null) {
            EditText text = (EditText) v.findViewById(R.id.txtTexto);
            String text = text.getText().toString();
            Log.d("APP", text + " " + i);
            String[] StrTmp = new String[3];
            StrTmp[0] = text.substring(0, 32);
            StrTmp[1] = text.substring(32, 64);
            StrTmp[2] = text.substring(64);
            responses.add(StrTmp);
         } else
               Log.d("APP", "NULL " + i + ". Position: " + cva.getPositions().get(i));
 }

使用日志,我看到屏幕中的 CardView 是预期值 return,但其他 return 为空。

我得到位置,将其保存在适配器中:

@Override
public void onBindViewHolder(final CardViewHolder viewHolder, int i)
{
    viewHolder.lblFound.setText(items.get(i).getSector());
    viewHolder.txtText.setText(items.get(i).getText());
    if(i==0)
    {
        viewHolder.txtText.setFocusable(false);
        viewHolder.txtText.setFocusableInTouchMode(false);
    }
    viewHolder.txtText.setTag(items.get(i).getId());
    positions.add(viewHolder.getAdapterPosition());
    Log.d("APP",String.valueOf(viewHolder.getAdapterPosition()));

}

而方法 getPositions() 是我在适配器中声明的方法 return 列表 "positions"。

public LinkedList<Integer> getPositions()
{
    return positions;
}

如有任何意见或帮助,我们将不胜感激。

您描述的行为(如果我理解正确的话)是 RecyclerView 的预期行为。它是 回收视图 ,这意味着它只会创建固定数量的视图(通常是显示的视图 + 1),这解释了为什么您无法获得未显示的项目的视图。

如果你想操作链接到视图的数据,你应该保留一个列表(或使用你已有的列表)并在 onBindViewHolder();

中操作它