Friday, March 23, 2012

ListView with Sections in android.- 2


ListView with Sections in android.- 2




Now the main java file which implements the logic for creating the listview with sections.
?
Drag and copy the code
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
package com.coderzheaven.pack;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class SectionListViewDemo extends Activity {
    ListView L1;
    myAdapter myadp;
    String last_item = "B";
    static final String[] labels_array = new String[] {
          "Afghanistan", "Albania",
          "Bahrain", "Bangladesh",
          "Cote d'Ivoire", "Cambodia",
          "Estonia", "Ethiopia", "Faeroe Islands",
          "Former Yugoslav Republic of Macedonia"
        };
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        L1 = (ListView)findViewById(R.id.list1);
        myadp = new myAdapter(this,labels_array);
        L1.setAdapter(myadp);
        L1.setOnItemClickListener(new OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
            }
        });
    }
    /* The adapter class.... */
    class myAdapter extends ArrayAdapter<String>
    {
       TextView label;
       View row;
       public myAdapter(Context context,String[] arr)
       {
            super(context, android.R.layout.simple_list_item_1, arr);
       }       
       public View getView(int position, View convertView, ViewGroup parent)
        {
               try{
                    LayoutInflater inflater=getLayoutInflater();
                    row = inflater.inflate(R.layout.lv_layout, parent, false);
                    LinearLayout L1 = (LinearLayout)row.findViewById(R.id.l1);
                    TextView header = new TextView(getApplicationContext());
                    L1.addView(header);
                    TextView label = new TextView(getApplicationContext());
                    L1.addView(label);
                    System.out.println("LAST : " + last_item);
                    header.setText(last_item);
                    label.setText(labels_array[position]);
                    label.setPadding(4, 1, 1, 1);
                    L1.setPadding(4, 4, 4, 4);
                    label.setTextColor(Color.BLACK); 
                    if(!labels_array[position].substring(0,1).equalsIgnoreCase(last_item)){
                        System.out.println("ADD :  " + last_item);
                        header.setBackgroundResource(R.drawable.customshape_header);
                        header.setPadding(4, 1, 1, 1);
                        row.setEnabled(false);
                        header.setTextColor(Color.BLACK);
                        header.setText(labels_array[position].substring(0,1));
                        //label.setVisibility(View.GONE);
                        //position-=2;
                    }else{
                        System.out.println("REM :  " + last_item);
                        header.setVisibility(View.GONE);
                    }
                    last_item = labels_array[position].substring(0,1);
               }catch(Exception e){
               }
            return row;
        }
    }
}
Now its ready to run the application.

No comments: