我試圖從EditText
獲取CardView
的信息,這些信息位於RecyclerView
中。 RecyclerView
只顯示4 CardView
的時間,總共有32 CardView
's。但是當我嘗試獲取未顯示的CardView
時,我得到空值而不是View
。所以,我的問題是,如何從RecyclerView
獲得所有View
?從CardViews獲取數據時爲空
我創建了一個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
中每個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));
}
使用日誌,我看到CardView
的是在屏幕返回預期的值,但其他人返回null。
我得到的位置,將其保存在適配器:
@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()
是我在返回列表中的「位置」適配器聲明的方法。
public LinkedList<Integer> getPositions()
{
return positions;
}
任何意見或幫助將不勝感激。
操縱它請張貼堆棧跟蹤,並顯示該行導致NPE。 –