从CardViews获取数据时为空

时间:2017-03-10 23:53:07

标签: android android-recyclerview cardview

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

我创建了一个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来自连接的文本,用户将编辑该文本。在用户编辑文本后,我会从每个TextView的{​​{1}}获取文字,按位置获取CardView并分割得到的文字。

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)); } 返回预期值,但其他人返回null。

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

CardView

方法@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())); } 是我在适配器中声明的方法,它返回List&#34; position&#34;。

getPositions()

任何评论或帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

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

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

中对其进行操作