0

I have a problem with get itemView in my adapter for preselect element in RecyclerView. In my activity I have:

    recyclerView = (RecyclerView) findViewById(R.id.activityRecicler);
    layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setHasFixedSize(true);

    adapter = new CustomRecycleAdapter(array, ReceiptNewActivity.this, arrayJson);
    recyclerView.setAdapter(adapter);
    int adapterLength = adapter.getItemCount();
    int length = arrayJson.length();
    for(int i = 0; i<adapterLength; i++){
        for(int j=0; j<length; j++){
            try {
                if(adapter.getItem(i).getInt("id") == arrayJson.getJSONObject(j).getInt("id")){

                    int value= arrayJson.getJSONObject(j).getInt("value");

                    float valueFloat = (float) value/250;
                    String valueString = String.valueOf(value);

                    //function in adapter for selected
                    adapter.setSelected(i, valueFloat);


                    RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForAdapterPosition(i);
                    View v= viewHolder.itemView;

                    adapter.setRowData(v, valueString);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

My problem is that RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForAdapterPosition(i); return always null. So How Can get itemView for position?

Nikhil
  • 3,711
  • 8
  • 32
  • 43
LorenzoBerti
  • 6,704
  • 8
  • 47
  • 89
  • 1
    You do this right after you have set the adapter. I can imagine that this doesn't work because your `ViewHolder`s are not created yet. Check if `onBindViewHolder` is already called to be sure. – 0xDEADC0DE Aug 26 '16 at 08:45
  • 1
    Hi. Are you trying to set data of the RecyclerView from outside the CustomRecycleAdapter class, or am I interpreting your code wrong? – Lev Aug 26 '16 at 09:04
  • Hi Lev, yes, It is a bad practice or create a problem? I do this (adapter.setSelected) because I want keep what element is selected in adapter. I have delete SetRowData and put in Onbind in adapter – LorenzoBerti Aug 26 '16 at 09:08
  • Yes 0xDEADc0DE, the answer is a mixed of your comment and @Lev comment, I have put setRowData, in my Adapter onBindViewHolder and it work. – LorenzoBerti Aug 26 '16 at 09:15
  • 1
    Hey Lorenzo, The Adapter is there for populating, setting data of your RecyclerView. You should do all the data setting inside the adapter code, onBindViewHolder is the place you should do that. – Lev Aug 26 '16 at 09:15
  • Ah, 30 seconds late. Glad to be of service :) – Lev Aug 26 '16 at 09:16
  • yes! :) Thankyou lev and 0xDEADC0DE, The answer is a mixed of yours comments, I have put setRowData, in my Adapter onBindViewHolder and it work. I'm a chicken. thankyou – LorenzoBerti Aug 26 '16 at 09:17
  • You can check this better implementation tutorial http://wiki.workassis.com/android-recyclerview-example/ – Bikesh M Aug 31 '16 at 07:50

0 Answers0