title: Using an adapter for two different situations
body: <p>I have a regular <code>RecyclerView</code> adapter that looks like this:</p>
  
  <pre><code>public class MessageAdapter extends RecyclerView.Adapter&lt;MessageAdapter.ViewHolder&gt; {
  
      private static final String TAG = "MessageAdapter";
  
      private Context context;
  
      private List&lt;Message&gt; messages;
  
      public MessageAdapter(Context context, List&lt;Message&gt; messages) {
          this.context = context;
          this.messages = messages;
      }
  
      public class ViewHolder extends RecyclerView.ViewHolder {
          public Message message;
  
          public TextView messageUser, messageText, messageTime;
  
          public ViewHolder(View v) {
              super(v);
  
              messageUser = (TextView) v.findViewById(R.id.messageUser);
              messageText = (TextView) v.findViewById(R.id.messageText);
              messageTime = (TextView) v.findViewById(R.id.messageTime);
          }
  
          public void setMessage(Message message) {
              this.message = message;
          }
      }
  
      @Override
      public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
          LayoutInflater inflater = LayoutInflater.from(context);
  
          View view = inflater.inflate(R.layout.message_layout, parent, false);
  
          ViewHolder viewHolder = new ViewHolder(view);
  
          return viewHolder;
      }
  
      @Override
      public void onBindViewHolder(final ViewHolder holder, int position) {
          Message message = messages.get(position);
  
          holder.setMessage(message);
  
          holder.messageUser.setText(message.getUser());
          holder.messageText.setText(message.getText());
          holder.messageTime.setText(message.getTime());
      }
  
      @Override
      public int getItemCount() {
          return messages.size();
      }
  
  }
  </code></pre>
  
  <p>I use this adapter to list all messages the user has received in the main activity.</p>
  
  <p>In addition to this, I am using the <a href="https://github.com/bmelnychuk/AndroidTreeView" rel="nofollow noreferrer">AndroidTreeView library</a> in a different part of my app that also displays the user's messages (but in a treeview layout).</p>
  
  <p>However, reading the documentation of this library, it says that in order to create an adapter for the treeview data, it must have the following format (taken from the docs):</p>
  
  <pre><code>public class MyHolder extends TreeNode.BaseNodeViewHolder&lt;IconTreeItem&gt; {
      ...
      @Override
      public View createNodeView(TreeNode node, IconTreeItem value) {
          final LayoutInflater inflater = LayoutInflater.from(context);
          final View view = inflater.inflate(R.layout.layout_profile_node, null, false);
          TextView tvValue = (TextView) view.findViewById(R.id.node_value);
          tvValue.setText(value.text);
  
          return view;
      }
      ...
      public static class IconTreeItem {
          public int icon;
          public String text;
      }
  }
  </code></pre>
  
  <p>So I created a second message adapter (specifically for the AndroidTreeView library) that looks like this:</p>
  
  <pre><code>public class MessageTreeAdapter extends TreeNode.BaseNodeViewHolder&lt;MessageTreeAdapter.TreeItem&gt; {
  
      private TextView messageUser;
      private TextView messageText;
      private TextView messageTime;
  
      public MessageTreeAdapter(Context context) {
          super(context);
      }
  
      @Override
      public View createNodeView(TreeNode node, TreeItem value) {
          final LayoutInflater inflater = LayoutInflater.from(context);
          final View view = inflater.inflate(R.layout.message_layout, null, false);
  
          Message message = value.message;
  
          messageUser = (TextView) v.findViewById(R.id.messageUser);
          messageText = (TextView) v.findViewById(R.id.messageText);
          messageTime = (TextView) v.findViewById(R.id.messageTime);
  
          messageUser.setText(message.getUser());
          messageText.setText(message.getText());
          messageTime.setText(message.getTime());
  
          return view;
      }
  
      public static class TreeItem {
          public Message message;
  
          public TreeItem(Message message) {
              this.message = message;
          }
      }
  }
  </code></pre>
  
  <p>But as you can see, I'm repeating what I already have in my first adapter, <code>MessageAdapter.java</code>. I want to avoid this.</p>
  
  <p>So my question is, how can I use a single adapter (<code>MessageAdapter.java</code>) for both situations so that I don't have to repeat any code.</p>