Search code examples
androidandroid-recyclerviewcardview

Null when getting data from CardViews


I'm trying to get information from EditText in CardView's that are in a RecyclerView. The RecyclerView just show 4 CardView's by time, and there are 32 CardView's in total. But when I try to get the CardView's that are not shown, I get null instead of the View. So, my question is, how can I get all the View's from the RecyclerView?

I created a RecyclerView, then I fill it up with CardView's using a custom Adapter, with information I get from a HID Connection.

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();
}

In the CardView's, I put a TextView with text gotten from the connection, that the user will edit. After the user edit the text, I get the text from the TextView in each CardView, getting the View by position and splitting the text gotten.

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));
 }

Using the Logs, I see that the CardView's that are in the screen return the value expected, but the others returns null.

I get the positions, saving it in the Adapter:

@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()));

}

And the method getPositions() is a method I declared in the adapter that returns the List "positions".

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

Any comment or help will be appreciated.


Solution

  • The behavior you describe (if I understand correctly) is the intended behavior of the RecyclerView. It's recycling views, meaning it will create only a fix number of views (usually the ones displayed + 1), which explains why you are not able to get the views of the items not displayed.

    If you want to manipulate data linked to views, you should keep a list (or use the one you already have) and manipulate it within the onBindViewHolder();