MargeAdapter ListView in Android

This will help you when populate listview data with one or two even more adapter. Combine two adapter in listview with diferent data.
Create new android project and make new xml file in your layout folder.

activity_main.xml
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:paddingBottom="@dimen/activity_vertical_margin"  
   android:paddingLeft="@dimen/activity_horizontal_margin"  
   android:paddingRight="@dimen/activity_horizontal_margin"  
   android:paddingTop="@dimen/activity_vertical_margin"  
   tools:context="com.example.margeadapter.MainActivity" >  
   
   <ListView android:id="@+id/listview"  
     android:layout_width="match_parent"  
     android:layout_height="match_parent"  
     android:divider="#999"  
     android:dividerHeight="1dp">  
       
   </ListView>  
   
 </RelativeLayout>  
   

list_row.xml
 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:layout_width="match_parent"  
   android:layout_height="50dp"  
   android:orientation="horizontal" >  
     
   <ImageView android:id="@+id/imageView"  
     android:layout_width="50dp"  
     android:layout_height="match_parent"  
     android:scaleType="fitXY"  
     android:src="@drawable/ic_launcher"/>  
     
  <LinearLayout android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:layout_weight="1"  
    android:orientation="vertical"  
    android:layout_marginLeft="5dp">  
      
    <TextView android:id="@+id/tvTitle"  
      android:layout_width="match_parent"  
      android:layout_height="wrap_content"  
      android:text="Title"  
      android:textSize="14dp"  
      android:textColor="#000"  
      android:textStyle="bold"  
      android:padding="4dp"/>  
      
    <TextView android:id="@+id/tvInfo"  
      android:layout_weight="1"  
      android:layout_width="match_parent"  
      android:layout_height="match_parent"  
      android:text="Info"  
      android:textSize="12dp"  
      android:textColor="#000"  
      android:padding="4dp"/>  
  </LinearLayout>  
 </LinearLayout>  
   

Now create new java class in your project package

MergeAdapter.java
 package com.example.margeadapter;  
   
 /***  
  Copyright (c) 2008-2009 CommonsWare, LLC  
  Portions (c) 2009 Google, Inc.  
    
  Licensed under the Apache License, Version 2.0 (the "License"); you may  
  not use this file except in compliance with the License. You may obtain  
  a copy of the License at  
   http://www.apache.org/licenses/LICENSE-2.0  
  Unless required by applicable law or agreed to in writing, software  
  distributed under the License is distributed on an "AS IS" BASIS,  
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  See the License for the specific language governing permissions and  
  limitations under the License.  
  */  
   
 import android.database.DataSetObserver;  
 import android.view.View;  
 import android.view.ViewGroup;  
 import android.widget.BaseAdapter;  
 import android.widget.ListAdapter;  
 import android.widget.SectionIndexer;  
   
 import java.util.ArrayList;  
 import java.util.Collections;  
 import java.util.List;  
   
 import com.example.margeadapter.SackOfViewsAdapter;  
   
 /**  
  * Adapter that merges multiple child adapters and views  
  * into a single contiguous whole.  
  *   
  * Adapters used as pieces within MergeAdapter must have  
  * view type IDs monotonically increasing from 0. Ideally,  
  * adapters also have distinct ranges for their row ids, as  
  * returned by getItemId().  
  *   
  */  
 public class MergeAdapter extends BaseAdapter implements SectionIndexer {  
  protected PieceStateRoster pieces=new PieceStateRoster();  
   
  /**  
   * Stock constructor, simply chaining to the superclass.  
   */  
  public MergeAdapter() {  
   super();  
  }  
   
  /**  
   * Adds a new adapter to the roster of things to appear in  
   * the aggregate list.  
   *   
   * @param adapter  
   *     Source for row views for this section  
   */  
  public void addAdapter(ListAdapter adapter) {  
   pieces.add(adapter);  
   adapter.registerDataSetObserver(new CascadeDataSetObserver());  
  }  
   
  /**  
   * Adds a new View to the roster of things to appear in  
   * the aggregate list.  
   *   
   * @param view  
   *     Single view to add  
   */  
  public void addView(View view) {  
   addView(view, false);  
  }  
   
  /**  
   * Adds a new View to the roster of things to appear in  
   * the aggregate list.  
   *   
   * @param view  
   *     Single view to add  
   * @param enabled  
   *     false if views are disabled, true if enabled  
   */  
  public void addView(View view, boolean enabled) {  
   ArrayList<View> list=new ArrayList<View>(1);  
   
   list.add(view);  
   
   addViews(list, enabled);  
  }  
   
  /**  
   * Adds a list of views to the roster of things to appear  
   * in the aggregate list.  
   *   
   * @param views  
   *     List of views to add  
   */  
  public void addViews(List<View> views) {  
   addViews(views, false);  
  }  
   
  /**  
   * Adds a list of views to the roster of things to appear  
   * in the aggregate list.  
   *   
   * @param views  
   *     List of views to add  
   * @param enabled  
   *     false if views are disabled, true if enabled  
   */  
  public void addViews(List<View> views, boolean enabled) {  
   if (enabled) {  
    addAdapter(new EnabledSackAdapter(views));  
   }  
   else {  
    addAdapter(new SackOfViewsAdapter(views));  
   }  
  }  
   
  /**  
   * Get the data item associated with the specified  
   * position in the data set.  
   *   
   * @param position  
   *     Position of the item whose data we want  
   */  
  @Override  
  public Object getItem(int position) {  
   for (ListAdapter piece : getPieces()) {  
    int size=piece.getCount();  
   
    if (position < size) {  
     return(piece.getItem(position));  
    }  
   
    position-=size;  
   }  
   
   return(null);  
  }  
   
  /**  
   * Get the adapter associated with the specified position  
   * in the data set.  
   *   
   * @param position  
   *     Position of the item whose adapter we want  
   */  
  public ListAdapter getAdapter(int position) {  
   for (ListAdapter piece : getPieces()) {  
    int size=piece.getCount();  
   
    if (position < size) {  
     return(piece);  
    }  
   
    position-=size;  
   }  
   
   return(null);  
  }  
   
  /**  
   * How many items are in the data set represented by this  
   * Adapter.  
   */  
  @Override  
  public int getCount() {  
   int total=0;  
   
   for (ListAdapter piece : getPieces()) {  
    total+=piece.getCount();  
   }  
   
   return(total);  
  }  
   
  /**  
   * Returns the number of types of Views that will be  
   * created by getView().  
   */  
  @Override  
  public int getViewTypeCount() {  
   int total=0;  
   
   for (PieceState piece : pieces.getRawPieces()) {  
    total+=piece.adapter.getViewTypeCount();  
   }  
   
   return(Math.max(total, 1)); // needed for  
                 // setListAdapter() before  
                 // content add'  
  }  
   
  /**  
   * Get the type of View that will be created by getView()  
   * for the specified item.  
   *   
   * @param position  
   *     Position of the item whose data we want  
   */  
  @Override  
  public int getItemViewType(int position) {  
   int typeOffset=0;  
   int result=-1;  
   
   for (PieceState piece : pieces.getRawPieces()) {  
    if (piece.isActive) {  
     int size=piece.adapter.getCount();  
    
     if (position < size) {  
      result=typeOffset + piece.adapter.getItemViewType(position);  
      break;  
     }  
    
     position-=size;  
    }  
      
    typeOffset+=piece.adapter.getViewTypeCount();  
   }  
   
   return(result);  
  }  
   
  /**  
   * Are all items in this ListAdapter enabled? If yes it  
   * means all items are selectable and clickable.  
   */  
  @Override  
  public boolean areAllItemsEnabled() {  
   return(false);  
  }  
   
  /**  
   * Returns true if the item at the specified position is  
   * not a separator.  
   *   
   * @param position  
   *     Position of the item whose data we want  
   */  
  @Override  
  public boolean isEnabled(int position) {  
   for (ListAdapter piece : getPieces()) {  
    int size=piece.getCount();  
   
    if (position < size) {  
     return(piece.isEnabled(position));  
    }  
   
    position-=size;  
   }  
   
   return(false);  
  }  
   
  /**  
   * Get a View that displays the data at the specified  
   * position in the data set.  
   *   
   * @param position  
   *     Position of the item whose data we want  
   * @param convertView  
   *     View to recycle, if not null  
   * @param parent  
   *     ViewGroup containing the returned View  
   */  
  @Override  
  public View getView(int position, View convertView, ViewGroup parent) {  
   for (ListAdapter piece : getPieces()) {  
    int size=piece.getCount();  
   
    if (position < size) {  
   
     return(piece.getView(position, convertView, parent));  
    }  
   
    position-=size;  
   }  
   
   return(null);  
  }  
   
  /**  
   * Get the row id associated with the specified position  
   * in the list.  
   *   
   * @param position  
   *     Position of the item whose data we want  
   */  
  @Override  
  public long getItemId(int position) {  
   for (ListAdapter piece : getPieces()) {  
    int size=piece.getCount();  
   
    if (position < size) {  
     return(piece.getItemId(position));  
    }  
   
    position-=size;  
   }  
   
   return(-1);  
  }  
   
  @Override  
  public int getPositionForSection(int section) {  
   int position=0;  
   
   for (ListAdapter piece : getPieces()) {  
    if (piece instanceof SectionIndexer) {  
     Object[] sections=((SectionIndexer)piece).getSections();  
     int numSections=0;  
   
     if (sections != null) {  
      numSections=sections.length;  
     }  
   
     if (section < numSections) {  
      return(position + ((SectionIndexer)piece).getPositionForSection(section));  
     }  
     else if (sections != null) {  
      section-=numSections;  
     }  
    }  
   
    position+=piece.getCount();  
   }  
   
   return(0);  
  }  
   
  @Override  
  public int getSectionForPosition(int position) {  
   int section=0;  
   
   for (ListAdapter piece : getPieces()) {  
    int size=piece.getCount();  
   
    if (position < size) {  
     if (piece instanceof SectionIndexer) {  
      return(section + ((SectionIndexer)piece).getSectionForPosition(position));  
     }  
   
     return(0);  
    }  
    else {  
     if (piece instanceof SectionIndexer) {  
      Object[] sections=((SectionIndexer)piece).getSections();  
   
      if (sections != null) {  
       section+=sections.length;  
      }  
     }  
    }  
   
    position-=size;  
   }  
   
   return(0);  
  }  
   
  @Override  
  public Object[] getSections() {  
   ArrayList<Object> sections=new ArrayList<Object>();  
   
   for (ListAdapter piece : getPieces()) {  
    if (piece instanceof SectionIndexer) {  
     Object[] curSections=((SectionIndexer)piece).getSections();  
   
     if (curSections != null) {  
      Collections.addAll(sections, curSections);  
     }  
    }  
   }  
   
   if (sections.size() == 0) {  
    return(new String[0]);  
   }  
   
   return(sections.toArray(new Object[0]));  
  }  
    
  public void setActive(ListAdapter adapter, boolean isActive) {  
   pieces.setActive(adapter, isActive);  
   notifyDataSetChanged();  
  }  
   
  public void setActive(View v, boolean isActive) {  
   pieces.setActive(v, isActive);  
   notifyDataSetChanged();  
  }  
   
  protected List<ListAdapter> getPieces() {  
   return(pieces.getPieces());  
  }  
   
  private static class PieceState {  
   ListAdapter adapter;  
   boolean isActive=true;  
   
   PieceState(ListAdapter adapter, boolean isActive) {  
    this.adapter=adapter;  
    this.isActive=isActive;  
   }  
  }  
   
  private static class PieceStateRoster {  
   protected ArrayList<PieceState> pieces=new ArrayList<PieceState>();  
   protected ArrayList<ListAdapter> active=null;  
   
   void add(ListAdapter adapter) {  
    pieces.add(new PieceState(adapter, true));  
   }  
     
   void setActive(ListAdapter adapter, boolean isActive) {  
    for (PieceState state : pieces) {  
     if (state.adapter==adapter) {  
      state.isActive=isActive;  
      active=null;  
      break;  
     }  
    }  
   }  
     
   void setActive(View v, boolean isActive) {  
    for (PieceState state : pieces) {  
     if (state.adapter instanceof SackOfViewsAdapter &&  
       ((SackOfViewsAdapter)state.adapter).hasView(v)) {  
      state.isActive=isActive;  
      active=null;  
      break;  
     }  
    }  
   }  
     
   List<PieceState> getRawPieces() {  
    return(pieces);  
   }  
   
   List<ListAdapter> getPieces() {  
    if (active == null) {  
     active=new ArrayList<ListAdapter>();  
   
     for (PieceState state : pieces) {  
      if (state.isActive) {  
       active.add(state.adapter);  
      }  
     }  
    }  
   
    return(active);  
   }  
  }  
   
  private static class EnabledSackAdapter extends SackOfViewsAdapter {  
   public EnabledSackAdapter(List<View> views) {  
    super(views);  
   }  
   
   @Override  
   public boolean areAllItemsEnabled() {  
    return(true);  
   }  
   
   @Override  
   public boolean isEnabled(int position) {  
    return(true);  
   }  
  }  
   
  private class CascadeDataSetObserver extends DataSetObserver {  
     
   @Override  
   public void onChanged() {  
    notifyDataSetChanged();  
   }  
   
   @Override  
   public void onInvalidated() {  
    notifyDataSetInvalidated();  
   }  
  }  
 }  
   
SackOfViewsAdapter.java
 package com.example.margeadapter;  
   
 import java.util.ArrayList;  
 import java.util.List;  
 import android.view.View;  
 import android.view.ViewGroup;  
 import android.widget.BaseAdapter;  
   
 public class SackOfViewsAdapter extends BaseAdapter {  
  private List<View> views=null;  
  /**  
  * Constructor creating an empty list of views, but with  
  * a specified count. Subclasses must override newView().  
  */  
  public SackOfViewsAdapter(int count) {  
  super();  
  views=new ArrayList<View>(count);  
  for (int i=0;i<count;i++) {  
  views.add(null);  
  }  
  }  
  /**  
  * Constructor wrapping a supplied list of views.  
  * Subclasses must override newView() if any of the elements  
  * in the list are null.  
  */  
  public SackOfViewsAdapter(List<View> views) {  
  super();  
  this.views=views;  
  }  
  /**  
  * Get the data item associated with the specified  
  * position in the data set.  
  * @param position Position of the item whose data we want  
  */  
  @Override  
  public Object getItem(int position) {  
  return(views.get(position));  
  }  
  /**  
  * How many items are in the data set represented by this  
  * Adapter.  
  */  
  @Override  
  public int getCount() {  
  return(views.size());  
  }  
  /**  
  * Returns the number of types of Views that will be  
  * created by getView().  
  */  
  @Override  
  public int getViewTypeCount() {  
  return(getCount());  
  }  
  /**  
  * Get the type of View that will be created by getView()  
  * for the specified item.  
  * @param position Position of the item whose data we want  
  */  
  @Override  
  public int getItemViewType(int position) {  
  return(position);  
  }  
  /**  
  * Are all items in this ListAdapter enabled? If yes it  
  * means all items are selectable and clickable.  
  */  
  @Override  
  public boolean areAllItemsEnabled() {  
  return(false);  
  }  
  /**  
  * Returns true if the item at the specified position is  
  * not a separator.  
  * @param position Position of the item whose data we want  
  */  
  @Override  
  public boolean isEnabled(int position) {  
  return(false);  
  }  
  /**  
  * Get a View that displays the data at the specified  
  * position in the data set.  
  * @param position Position of the item whose data we want  
  * @param convertView View to recycle, if not null  
  * @param parent ViewGroup containing the returned View  
  */  
  @Override  
  public View getView(int position, View convertView,  
  ViewGroup parent) {  
  View result=views.get(position);  
  if (result==null) {  
  result=newView(position, parent);  
  views.set(position, result);  
  }  
  return(result);  
  }  
  /**  
  * Get the row id associated with the specified position  
  * in the list.  
  * @param position Position of the item whose data we want  
  */  
  @Override  
  public long getItemId(int position) {  
  return(position);  
  }  
  public boolean hasView(View v) {  
  return(views.contains(v));  
  }  
  /**  
  * Create a new View to go into the list at the specified  
  * position.  
  * @param position Position of the item whose data we want  
  * @param parent ViewGroup containing the returned View  
  */  
  protected View newView(int position, ViewGroup parent) {  
  throw new RuntimeException("You must override newView()!");  
  }  
  }  

ListAdapter1.java

 package com.example.margeadapter;  
   
 import android.content.Context;  
 import android.view.LayoutInflater;  
 import android.view.View;  
 import android.view.ViewGroup;  
 import android.widget.BaseAdapter;  
 import android.widget.ImageView;  
 import android.widget.TextView;  
   
 public class ListAdapter1 extends BaseAdapter{  
   
  private Context mContext;  
  private String[] Title;  
  private String[] Info;  
  private int[] Image;  
  public ListAdapter1(Context context, String[] title, String[] info, int[] image) {  
   mContext = context;  
   this.Title = title;  
   this.Info = info;  
   this.Image = image;  
   
  }  
   
  @Override  
  public int getCount() {  
   // TODO Auto-generated method stub  
    return Title.length;  
  }  
   
  @Override  
  public Object getItem(int position) {  
   // TODO Auto-generated method stub  
   return null;  
  }  
   
  @Override  
  public long getItemId(int position) {  
   // TODO Auto-generated method stub  
   return 0;  
  }  
   
  @Override  
  public View getView(int position, View convertView, ViewGroup parent) {  
   // TODO Auto-generated method stub  
   View view;  
   LayoutInflater inflater = (LayoutInflater) mContext  
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
   if (convertView == null) {  
    view = new View(mContext);  
    view = inflater.inflate(R.layout.list_row, null);  
      
    TextView tvTitle = (TextView) view.findViewById(R.id.tvTitle);  
    TextView tvInfo = (TextView) view.findViewById(R.id.tvInfo);  
    ImageView imageView = (ImageView) view.findViewById(R.id.imageView);  
      
    tvTitle.setText(Title[position]);  
    tvInfo.setText(Info[position]);  
    imageView.setImageResource(Image[position]);  
   }  
   else {  
       view = convertView;  
     }  
     
   return view;  
  }  
   
 }  
   

ListAdapter2.java

 package com.example.margeadapter;  
   
 import android.content.Context;  
 import android.view.LayoutInflater;  
 import android.view.View;  
 import android.view.ViewGroup;  
 import android.widget.BaseAdapter;  
 import android.widget.ImageView;  
 import android.widget.TextView;  
   
 public class ListAdapter2 extends BaseAdapter{  
   
  private Context mContext;  
  private String[] Title;  
  private String[] Info;  
  private int[] Image;  
  public ListAdapter2(Context context, String[] title, String[] info, int[] image) {  
   mContext = context;  
   this.Title = title;  
   this.Info = info;  
   this.Image = image;  
   
  }  
   
  @Override  
  public int getCount() {  
   // TODO Auto-generated method stub  
    return Title.length;  
  }  
   
  @Override  
  public Object getItem(int position) {  
   // TODO Auto-generated method stub  
   return null;  
  }  
   
  @Override  
  public long getItemId(int position) {  
   // TODO Auto-generated method stub  
   return 0;  
  }  
   
  @Override  
  public View getView(int position, View convertView, ViewGroup parent) {  
   // TODO Auto-generated method stub  
   View view;  
   LayoutInflater inflater = (LayoutInflater) mContext  
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
   if (convertView == null) {  
    view = new View(mContext);  
    view = inflater.inflate(R.layout.list_row, null);  
      
    TextView tvTitle = (TextView) view.findViewById(R.id.tvTitle);  
    TextView tvInfo = (TextView) view.findViewById(R.id.tvInfo);  
    ImageView imageView = (ImageView) view.findViewById(R.id.imageView);  
      
    tvTitle.setText(Title[position]);  
    tvInfo.setText(Info[position]);  
    imageView.setImageResource(Image[position]);  
   }  
   else {  
       view = convertView;  
     }  
     
   return view;  
  }  
 }  
   

ListAdapter3.java

 package com.example.margeadapter;  
   
 import android.content.Context;  
 import android.view.LayoutInflater;  
 import android.view.View;  
 import android.view.ViewGroup;  
 import android.widget.BaseAdapter;  
 import android.widget.ImageView;  
 import android.widget.TextView;  
   
 public class ListAdapter3 extends BaseAdapter {  
   
  private Context mContext;  
  private String[] Title;  
  private String[] Info;  
  private int[] Image;  
  public ListAdapter3(Context context, String[] title, String[] info, int[] image) {  
   mContext = context;  
   this.Title = title;  
   this.Info = info;  
   this.Image = image;  
   
  }  
   
  @Override  
  public int getCount() {  
   // TODO Auto-generated method stub  
    return Title.length;  
  }  
   
  @Override  
  public Object getItem(int position) {  
   // TODO Auto-generated method stub  
   return null;  
  }  
   
  @Override  
  public long getItemId(int position) {  
   // TODO Auto-generated method stub  
   return 0;  
  }  
   
  @Override  
  public View getView(int position, View convertView, ViewGroup parent) {  
   // TODO Auto-generated method stub  
   View view;  
   LayoutInflater inflater = (LayoutInflater) mContext  
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
   if (convertView == null) {  
    view = new View(mContext);  
    view = inflater.inflate(R.layout.list_row, null);  
      
    TextView tvTitle = (TextView) view.findViewById(R.id.tvTitle);  
    TextView tvInfo = (TextView) view.findViewById(R.id.tvInfo);  
    ImageView imageView = (ImageView) view.findViewById(R.id.imageView);  
      
    tvTitle.setText(Title[position]);  
    tvInfo.setText(Info[position]);  
    imageView.setImageResource(Image[position]);  
   }  
   else {  
       view = convertView;  
     }  
     
   return view;  
  }  
 }  
   

Update your MainActivity.java with code below

MainActivity.java

 package com.example.margeadapter;  
   
 import android.app.Activity;  
 import android.os.Bundle;  
 import android.view.View;  
 import android.widget.AdapterView;  
 import android.widget.TextView;  
 import android.widget.Toast;  
 import android.widget.AdapterView.OnItemClickListener;  
 import android.widget.ListView;  
   
   
 public class MainActivity extends Activity {  
   
  String[] title = {"Title 1","Title 2", "Title 3", "Title 4", "Title 5"};  
  String[] info = {"Info 1","Info 2", "Info 3", "Info 4", "Info 5"};  
  int[] image = {R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher};  
    
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
       
     final ListView list = (ListView) findViewById(R.id.listview);  
       
     ListAdapter1 listAdapter1 = new ListAdapter1(getApplicationContext(), title, info, image);  
     ListAdapter2 listAdapter3 = new ListAdapter2(getApplicationContext(), title, info, image);  
     ListAdapter3 listAdapter2 = new ListAdapter3(getApplicationContext(), title, info, image);  
       
     MergeAdapter mergeAdapter = new MergeAdapter();  
       
     mergeAdapter.addAdapter(listAdapter1);  
     mergeAdapter.addAdapter(listAdapter2);  
     mergeAdapter.addAdapter(listAdapter3);  
       
     list.setAdapter(mergeAdapter);  
       
     list.setOnItemClickListener(new OnItemClickListener() {  
   
    @Override  
    public void onItemClick(AdapterView<?> parent, View view,  
      int position, long id) {  
     // TODO Auto-generated method stub   
     TextView tv = (TextView) view.findViewById(R.id.tvTitle);  
     Toast.makeText(MainActivity.this, tv.getText().toString(), Toast.LENGTH_SHORT).show();  
    }  
   });  
   }  
 }  
   

Comments

Popular posts from this blog

Tutorial Integration Firebase With Admob on Android

Firebase Configuration in Android

How To Generate A Random String Alphabet And Number in Java Android