SoFunction
Updated on 2025-03-11

Multi-column template instance code of Android listview

Android listview multi-column template

In listview, you can make multiple columns of template effects. The key is to have the template version of listview, such as the following:

<LinearLayout  
  android:  
  android:layout_height="fill_parent"  
  android:layout_width="fill_parent" 
  xmlns:andro> 
 
  <TextView 
    android: 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:text="First" 
    android:layout_weight="1"> 
  </TextView> 
   
  <TextView 
    android: 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:text="Second" 
    android:layout_weight="2"> 
  </TextView> 
   
  <TextView 
    android: 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:text="Third" 
    android:layout_weight="1"> 
  </TextView> 
   
  <TextView 
    android: 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:text="Fourth" 
    android:layout_weight="1"> 
  </TextView> 
</LinearLayout> 

:

public class listviewAdapter extends BaseAdapter 
{ 
  public ArrayList<HashMap<String,String>> list; 
  Activity activity; 
   
  public listviewAdapter(Activity activity, ArrayList<HashMap<String,String>> list) { 
    super(); 
     = activity; 
     = list; 
  } 
 
   
  public int getCount() { 
    // TODO Auto-generated method stub 
    return (); 
  } 
 
   
  public Object getItem(int position) { 
    // TODO Auto-generated method stub 
    return (position); 
  } 
 
 
  public long getItemId(int position) { 
    // TODO Auto-generated method stub 
    return 0; 
  } 
 
  private class ViewHolder { 
      TextView txtFirst; 
      TextView txtSecond; 
      TextView txtThird; 
      TextView txtFourth; 
   } 
 
    
 
  public View getView(int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 
     
    // TODO Auto-generated method stub 
        ViewHolder holder; 
        LayoutInflater inflater = (); 
 
        if (convertView == null) 
        { 
          convertView = (.listview_row, null); 
          holder = new ViewHolder(); 
           = (TextView) (); 
           = (TextView) (); 
           = (TextView) (); 
           = (TextView) (); 
          (holder); 
        } 
        else 
        { 
          holder = (ViewHolder) (); 
        } 
 
        HashMap<String, String> map = (position); 
        ((FIRST_COLUMN)); 
        ((SECOND_COLUMN)); 
        ((THIRD_COLUMN)); 
        ((FOURTH_COLUMN)); 
 
      return convertView; 
  } 

Main program:

public class MultiColumnActivity extends Activity  
{ 
  private ArrayList<HashMap<String,String>> list; 
   
  public void onCreate(Bundle savedInstanceState)  
  { 
    (savedInstanceState); 
    setContentView(); 
 
    ListView lview = (ListView) findViewById(); 
    populateList(); 
    listviewAdapter adapter = new listviewAdapter(this, list); 
    (adapter); 
  }   
 
  private void populateList() { 
     
    list = new ArrayList<HashMap<String,String>>(); 
     
    HashMap<String,String> temp = new HashMap<String,String>(); 
      (FIRST_COLUMN,"Colored Notebooks"); 
      (SECOND_COLUMN, "By NavNeet"); 
      (THIRD_COLUMN, "Rs. 200"); 
      (FOURTH_COLUMN, "Per Unit"); 
    (temp); 
     
    HashMap<String,String> temp1 = new HashMap<String,String>(); 
      (FIRST_COLUMN,"Diaries"); 
      (SECOND_COLUMN, "By Amee Products"); 
      (THIRD_COLUMN, "Rs. 400"); 
      (FOURTH_COLUMN, "Per Unit"); 
    (temp1); 
     
    HashMap<String,String> temp2 = new HashMap<String,String>(); 
      (FIRST_COLUMN,"Note Books and Stationery"); 
      (SECOND_COLUMN, "By National Products"); 
      (THIRD_COLUMN, "Rs. 600"); 
      (FOURTH_COLUMN, "Per Unit"); 
    (temp2); 
     
    HashMap<String,String> temp3 = new HashMap<String,String>(); 
      (FIRST_COLUMN,"Corporate Diaries"); 
      (SECOND_COLUMN, "By Devarsh Prakashan"); 
      (THIRD_COLUMN, "Rs. 800"); 
      (FOURTH_COLUMN, "Per Unit"); 
    (temp3); 
     
    HashMap<String,String> temp4 = new HashMap<String,String>(); 
      (FIRST_COLUMN,"Writing Pad"); 
      (SECOND_COLUMN, "By TechnoTalaktive Pvt. Ltd."); 
      (THIRD_COLUMN, "Rs. 100"); 
      (FOURTH_COLUMN, "Per Unit"); 
    (temp4); 
  } 
} 

Thank you for reading, I hope it can help you. Thank you for your support for this site!