Custom ListView code...

To make a custom ListView here we will use two XML layout.One is the main layout,where the ListView widget is taken and another is simple_list XML,where a TextView with a background.The ListView items will be shown above this TextView.So the code of main is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
<ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/statelist"></ListView>
</LinearLayout>


And the code of simple_list.xml is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:padding="1dip" android:layout_height="wrap_content">
   
    <TextView android:id="@+id/toptext" android:gravity="center_vertical" android:layout_height="wrap_content" android:padding="5dip" android:layout_width="fill_parent" android:textColor="#000000" android:background="@drawable/textfield" android:focusable="true"/>

</LinearLayout>

Here in the drawable folder of the project we take a  image that is linked for the TextView background.In this project the image name is "textfield".(Remember,always give the image name a lower case name with no special character and image format should be png.).

Now it is time for the code.For this we take a array of string with different strings.(Here we use the states of USA).Now watch the code carefully:

package com.custom.list;




import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;



public class MyList extends Activity {
    /** Called when the activity is first created. */
    ListView lv_statelist;
    String[] state={"Alaska","Alabama","Arkansas","Arizona",
            "California","Colorado","Connecticut","Dist Of Col",
            "Delaware","Florida","Georgia","Hawaii","Iowa","Idaho",
            "Illinois","Indiana","Kansas","Kentucky","Louisiana",
            "Massachusetts","Maryland","Maine","Michigan","Minnesota",
            "Missouri","Mississippi","Montana","North Carolina",
            "North Dakota","Nebraska","New Hampshire","New Jersey",
            "New Mexico","Nevada","New York","Ohio","Oklahoma",
            "Oregon","Pennsylvania","Rhode Island","South Carolina",
            "South Dakota","Tennessee","Texas","Utah","Virginia",
            "Vermont","Washington","Wisconsin","West Virginia",
            "Wyoming"};
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
     lv_statelist=(ListView)findViewById(R.id.statelist);
       
        implementListView(state);       
       
    }
   
    private void implementListView(String[] state2) {
               
        ArrayAdapter<String> ad=new StorageListAdapter(MyList.this,R.layout.simple_list, state);
        lv_statelist.setAdapter(ad);
        lv_statelist.setTextFilterEnabled(true);
       
    }
   
    private class StorageListAdapter extends ArrayAdapter<String> {

        String[] state;
       
        public StorageListAdapter(Context context, int textViewResourceId, String[] state ) {
                super(context, textViewResourceId, state);
                this.state = state;               
        }

        public View getView(int position, View convertView, ViewGroup parent) {
           
            final int p=position;
                View v = convertView;
                //v = convertView;
                if (v == null) {
                    LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = vi.inflate(R.layout.simple_list, null);
                }
                String o = state[position]; 
               System.out.println("----"+state[position]+"-------");
               final TextView tt = (TextView) v.findViewById(R.id.toptext);
                if (o != null) {
                   
                    //tt = (TextView) v.findViewById(R.id.toptext);
                    if (tt != null) {
                        tt.setText(o);                               
                       
                        }
                                    
                }
               
                v.setOnClickListener(new OnClickListener(){

                    @Override
                    public void onClick(View v) {
                    System.out.println("Postion is:"+p+tt.getText().toString());
                       
                    }
                   
                });
                return v;
            }
        }
}

Here the strings will be shown in ListView on the top of TextView (with background image) of simple_list.xml.When you scroll through the ListView items you will get string name and it's ListView position number in the logcat view.This is achieved by the System.out.println() method written on the code.

Happy coding.....

No comments:

Post a Comment