Android ListView is Viewgroup of that create list of scrollable item.A view that show item in a vertically  scrolling list.

Now create project with Android Simple Listview.
create row.xml file in res->layout folder of your project.

row.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp"
android:textSize="15dp"
</TextView>

now open your MainActivity.java file in your project src folder. and 

public class MainActivity extends ListActivity

see here when we extending ListActivity to MainActivity then thet layout file must have ListView with id android:id=”@android:id/list”

Main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ListView>
</RelativeLayout>

Now using ArrayAdapter you can bind data with listview.ArrayAdapter is class that works to bind data to listview.You pass data in Constructor of this class.

private String[] INDIAN_STATE = { "Gujarat", "Andra Pradesh","Arunachal Pradesh", "Assam", "Bihar", "Chhattisgarh", "Goa","Haryana", "Himachal Pradesh", "Jammu and Kashmir", "Jharkhand","Karnataka", "Kerala", "Madya Pradesh", "Maharashtra", "Manipur", "Meghalaya", "Mizoram", "Nagaland", "Orissa", "Punjab", "Rajasthan", "Sikkim", "Tamil Nadu", "Tripura", "Uttaranchal", "Uttar Pradesh", "West Bengal" };

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.row, INDIAN_STATE);

now you just call setListAdapter to set Adapter to listview.

setListAdapter(adapter);

its display the indian state in listview and you can change your color of state name and background using row.xml file.they have textview,change its color or background.

Now,when listview is displayed its more important how to check which item clicked in listview and get selected or clicked value.there are OnItemClickListener for ListView.

but when you extends ListActivity then just ovveride onListItemClick  method.and there first parameter is listview.second is view which is row or item view.third is position of item.and last is id of row or item.

we can retrieve value of item.


@Override
protected void onListItemClick(ListView l, View v, int position, long id) {    
   super.onListItemClick(l, v, position, id);

   String str =l.getItemAtPosition(position).toString();
   Toast.makeText(MainActivity.this, str,Toast.LENGTH_SHORT).show();

}

now we discuss how to display listview without extending ListActivity. When we extends Activity there is no need to(not compulsory) your ListView have id android:id=”@android:id/list”. You can use your own list nameandroid:id=”@+id/mylist”

 you can get Listview using,

 ListView listView = (ListView)findViewById(R.id.mylist);

And set Adapter and OnItemClickListener to listView.

listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> l, View v, int position,long id) {
String str = l.getItemAtPosition(position).toString();
Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
}
});

DownLoad Full Source Code From Here
DownLoad Source Code

Comments

comments

One comment

  • mobile access
    mobile access

    Hi great website! Does running a blog like this require a great deal of work?
    I have virtually no understanding of computer
    programming however I was hoping to start my own blog in the near future.
    Anyways, should you have any suggestions or techniques for
    new blog owners please share. I understand this is off topic nevertheless
    I just needed to ask. Appreciate it!

Leave a Reply

Your email address will not be published. Required fields are marked *
You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Comments

comments