title: Edit RecyclerView item programatically in Android
body: <p>So I have this app in which I have to make a <code>RecyclerView</code> which contains a list of items that can be deleted/edited e.t.c.</p>
  
  <p>I followed a tutorial on youtube and I made a custom <code>CardView</code> item on another layout and a custom adapter for that item.</p>
  
  <p>Thing is, depending on the state of the item, I have to change something(ex. background color or text color).</p>
  
  <p>When I do that in the <code>RecyclerView</code> activity I get <code>NullPointerException</code> even if I have the id's.</p>
  
  <p>How can I edit those <code>TextViews</code> inside the programmatically generated list of items in the moment I make the Retrofit call?</p>
  
  <pre><code>boolean isEnded;
                  if(!endedAt.equals("null"))
                  {
                      endedAt = endedAt.substring(endedAt.indexOf("T") + 1, endedAt.lastIndexOf(":"));
                      isEnded=true;
                  }
                  else
                  {
                      endedAt="ongoing";
                      isEnded=false;
                  }
                  item.timeDifference=createdAt+" - "+endedAt;
                  if(externalSystem.equals("null"))
                  {
                      item.externalSystem="";
                  }
                  else
                  {
                      item.externalSystem = externalSystem;
                  }
                  Log.i("attr",externalSystem);
                  items.add(item);
  
                  itemAdapter=new ItemAdapter(getApplicationContext(), items);
                  recyclerView.setAdapter(itemAdapter);
                  if(isEnded) {
                  error-&gt;    externalSystemView.setTextColor(Color.BLACK);
                  }
  </code></pre>
  
  <p>The app is rather big, but I think you can get the idea from this piece of code.</p>
  
  <p>Here is the error: Attempt to invoke virtual method 'void android.widget.TextView.setTextColor(int)' on a null object reference</p>
  
  <pre><code>public class ItemAdapter extends     
  RecyclerView.Adapter&lt;ItemAdapter.ItemViewHolder&gt;{
  
  private Context context;
  private  ArrayList&lt;Item&gt; itemList;
  
  public ItemAdapter(Context context, ArrayList&lt;Item&gt; itemList)
  {
      this.context=context;
      this.itemList=itemList;
  }
  
  @Override
  public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      LayoutInflater layoutInflater=LayoutInflater.from(parent.getContext());
      View view=layoutInflater.inflate(R.layout.item_layout,parent,false);
      ItemViewHolder itemViewHolder=new ItemViewHolder(view);
      return itemViewHolder;
  }
  
  @Override
  public void onBindViewHolder(ItemViewHolder holder, int position) {
      Item item=itemList.get(position);
      holder.timeDifference.setText(item.timeDifference);
      holder.title.setText(item.title);
      holder.timeCounter.setText(item.timeCounter);
      holder.externalSystem.setText(item.externalSystem);
      holder.type.setText(item.type);
      holder.project.setText(item.project);
      MY IDEA
      "holder.timeDifference.setTextColor(Color.parseColor(item.color));"
  }
  
  @Override
  public int getItemCount() {
      if(itemList!=null)
          return itemList.size();
      else
          return 0;
  }
  
  
      public static class ItemViewHolder extends RecyclerView.ViewHolder
  {
      public CardView cardViewItem;
      public TextView title;
      public TextView project;
      public TextView externalSystem;
      public TextView timeDifference;
      public TextView timeCounter;
      public TextView type;
  
      public ItemViewHolder(View itemView)
      {
          super(itemView);
          cardViewItem=(CardView)itemView.findViewById(R.id.card_view_item);
          title=(TextView)itemView.findViewById(R.id.title);
          project=(TextView)itemView.findViewById(R.id.project);
          externalSystem=
          (TextView)itemView.findViewById(R.id.external_system);
          timeDifference=   
          (TextView)itemView.findViewById(R.id.time_difference);
          timeCounter=(TextView)itemView.findViewById(R.id.time_counter);
          type=(TextView)itemView.findViewById(R.id.type);
      }
  </code></pre>
  
  <p><strong>EDIT: I think I found a way, but I don't know if it's the best one</strong></p>