Monday, July 21, 2014

Android : Listview section header with custom adapter example

Android : Listview section header with  custom adapter example
This post about how to add the section for listview .see the below example.
Listview have each section different size of values.

Output

xml source code
activity_main.xml


<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.listviewsectionheader.MainActivity"
    tools:ignore="MergeRootFrame" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</FrameLayout>





adapter_layout.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="70dip"
    android:id="@+id/linerLayout"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:padding="10dip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="" />

</LinearLayout>



Class codes
 Creating getter and setter method for values and section;



private class SectionStructure{
      
       public String sectionName;
       public String sectionValue;
              public String getSectionName() {
                     return sectionName;
              }
              public void setSectionName(String sectionName) {
                     this.sectionName = sectionName;
              }
              public String getSectionValue() {
                     return sectionValue;
              }
              public void setSectionValue(String sectionValue) {
                     this.sectionValue = sectionValue;
              }
    }


Adapter Class


public class AdapterClass extends BaseAdapter{

              @Override
              public int getCount() {
                     // TODO Auto-generated method stub
                     return sectionList.size();
              }

              @Override
              public Object getItem(int arg0) {
                     // TODO Auto-generated method stub
                     return arg0;
              }

              @Override
              public long getItemId(int arg0) {
                     // TODO Auto-generated method stub
                     return arg0;
              }

              @Override
              public View getView(int arg0, View arg1, ViewGroup arg2) {
                    
                     View vi = arg1;
                    
                     vi = inf.inflate(R.layout.adapter_list, null);
                    
                     TextView textView =(TextView)vi.findViewById(R.id.textView1);
                     LinearLayout linearLayout = (LinearLayout)vi.findViewById(R.id.linerLayout);
                     if(sectionList.get(arg0).getSectionValue() !=null && sectionList.get(arg0).getSectionValue().equalsIgnoreCase("")){
                           textView.setText(sectionList.get(arg0).getSectionName());
                           linearLayout.setBackgroundColor(Color.GRAY);
                          
                     }
                     else{
                           textView.setText(sectionList.get(arg0).getSectionValue());
                           linearLayout.setBackgroundColor(Color.WHITE);
                     }
                    
                     return vi;
              }
      
    }
   
   


Full actiivity source code



package com.example.listviewsectionheader;

import java.util.ArrayList;

import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.graphics.Color;
import android.os.Bundle;

public class MainActivity extends ActionBarActivity {
     ListView listView;
     String[] sectionHeader = {"Section 1","Section 2","Section 3"};
     String[] values = {"iamvijayakumar.blogspot.com","blog 2","Blog 3","Blog 4"};
     ArrayList<SectionStructure> sectionList = new ArrayList<MainActivity.SectionStructure>();
     SectionStructure str;
     LayoutInflater inf;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        inf = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
        listView = (ListView)findViewById(R.id.listView1);
       
        for(int i=0; i<sectionHeader.length; i++){
              for(int j=0; j<values.length+1; j++){
                     str = new SectionStructure();
                     if(j==0){
                           str.setSectionName(sectionHeader[i]);
                           str.setSectionValue("");
                           sectionList.add(str);
                     }
                     else{
                          
                           if(i ==1 && j== 2){
                          
                           }else{
                                  str.setSectionName("");
                           str.setSectionValue(values[j-1]);
                           sectionList.add(str);
                           }
                                 
                     }
                    
                    
              }
        }

        listView.setAdapter(new AdapterClass());
       
    }

   
    public class AdapterClass extends BaseAdapter{

              @Override
              public int getCount() {
                     // TODO Auto-generated method stub
                     return sectionList.size();
              }

              @Override
              public Object getItem(int arg0) {
                     // TODO Auto-generated method stub
                     return arg0;
              }

              @Override
              public long getItemId(int arg0) {
                     // TODO Auto-generated method stub
                     return arg0;
              }

              @Override
              public View getView(int arg0, View arg1, ViewGroup arg2) {
                    
                     View vi = arg1;
                    
                     vi = inf.inflate(R.layout.adapter_list, null);
                    
                     TextView textView =(TextView)vi.findViewById(R.id.textView1);
                     LinearLayout linearLayout = (LinearLayout)vi.findViewById(R.id.linerLayout);
                     if(sectionList.get(arg0).getSectionValue() !=null && sectionList.get(arg0).getSectionValue().equalsIgnoreCase("")){
                           textView.setText(sectionList.get(arg0).getSectionName());
                           linearLayout.setBackgroundColor(Color.GRAY);
                          
                     }
                     else{
                           textView.setText(sectionList.get(arg0).getSectionValue());
                           linearLayout.setBackgroundColor(Color.WHITE);
                     }
                    
                     return vi;
              }
      
    }
   
   
   
   
   
    private class SectionStructure{
      
       public String sectionName;
       public String sectionValue;
              public String getSectionName() {
                     return sectionName;
              }
              public void setSectionName(String sectionName) {
                     this.sectionName = sectionName;
              }
              public String getSectionValue() {
                     return sectionValue;
              }
              public void setSectionValue(String sectionValue) {
                     this.sectionValue = sectionValue;
              }
    }
}



Check out this may be help you

Related Posts Plugin for WordPress, Blogger...