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"?>
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"
don't forget this because it cause exception your content must have listview whose attributes or id is android.R.id.list.
Main.xml
<?xml version="1.0" encoding="utf-8"?>
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.Using
String str =l.getItemAtPosition(position).toString();
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 name
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
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>
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"
don't forget this because it cause exception your content must have listview whose attributes or id is android.R.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>
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.Using
String str =l.getItemAtPosition(position).toString();
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 name
android: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
No comments:
Post a Comment