ListView is important view in android and it handle a lot of Design View. In this tutorial We deal all about ListView. So Start From Scratch.
ListView is a view group that displays a list of scrollable items. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database query and converts each item result into a view that's placed into the list.
What is Adapter ?
ListView gets the data to display via an adapter. An adapter extend BaseAdapter and is responsible for providing the data model for the list and for converting the data into the fields of the list.
Android has two standard adapters, ArrayAdapter and CursorAdapter.
ArrayAdapter can handle data based on Arrays or Lists while CursorAdapter handle database related data.
You can develop your own Adapter by extending these classes or the BaseAdapter class.
The most important method of the Adapter is getView().
The getView() method is called for every line in the ListView to determine which data should be displayed in the line and how.
Our First Programme is Simple ListView To Display Data from an Array.
The output of simplelist View look likes this ....
2nd Step Coding
Now open Eclipse and create a new Android Project and named it ListView1. After creating project you got project folder. In that folder there is a folder named as src and layout . At first open layout folder and click on main.xml file. after opening it add following line to create three Buttons as shown ion First screen shot image.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:textStyle="bold"
android:text="ListView Home Page"
tools:context=".MainActivity" />
<Button
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="simpleList"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="simpleList"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="simpleList"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
After pasting above code now you open src folder and put following code in MainActivity class.
public class MainActivity extends Activity {
//Global Variable
Button listview1, listview2, listview3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Getting View
listview1 = (Button)findViewById(R.id.textView1);
listview2 = (Button)findViewById(R.id.textView2);
listview3 = (Button)findViewById(R.id.textView3);
listview1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(getBaseContext(), simleLists.class);
startActivity(i);
}
});
listview2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(getBaseContext(), CustomList.class);
startActivity(i);
}
});
listview3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(getBaseContext(), CompolexList.class);
startActivity(i);
}
});
}
}
For SimpleList View
Now Create a class in your src Folder and write following code to display simple List
public class simleLists extends ListActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.simple);
String data [] = {"Name of Fruits", "Apple","Mango","Grapes","Pineapple","Goava"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1,data);
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String item = (String) getListAdapter().getItem(position);
Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();
}
}
ListView is a view group that displays a list of scrollable items. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database query and converts each item result into a view that's placed into the list.
What is Adapter ?
ListView gets the data to display via an adapter. An adapter extend BaseAdapter and is responsible for providing the data model for the list and for converting the data into the fields of the list.
Android has two standard adapters, ArrayAdapter and CursorAdapter.
ArrayAdapter can handle data based on Arrays or Lists while CursorAdapter handle database related data.
You can develop your own Adapter by extending these classes or the BaseAdapter class.
The most important method of the Adapter is getView().
The getView() method is called for every line in the ListView to determine which data should be displayed in the line and how.
Our First Programme is Simple ListView To Display Data from an Array.
The output of simplelist View look likes this ....
2nd Step Coding
Now open Eclipse and create a new Android Project and named it ListView1. After creating project you got project folder. In that folder there is a folder named as src and layout . At first open layout folder and click on main.xml file. after opening it add following line to create three Buttons as shown ion First screen shot image.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:textStyle="bold"
android:text="ListView Home Page"
tools:context=".MainActivity" />
<Button
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="simpleList"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="simpleList"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="simpleList"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
After pasting above code now you open src folder and put following code in MainActivity class.
public class MainActivity extends Activity {
//Global Variable
Button listview1, listview2, listview3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Getting View
listview1 = (Button)findViewById(R.id.textView1);
listview2 = (Button)findViewById(R.id.textView2);
listview3 = (Button)findViewById(R.id.textView3);
listview1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(getBaseContext(), simleLists.class);
startActivity(i);
}
});
listview2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(getBaseContext(), CustomList.class);
startActivity(i);
}
});
listview3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(getBaseContext(), CompolexList.class);
startActivity(i);
}
});
}
}
For SimpleList View
Now Create a class in your src Folder and write following code to display simple List
public class simleLists extends ListActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.simple);
String data [] = {"Name of Fruits", "Apple","Mango","Grapes","Pineapple","Goava"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1,data);
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String item = (String) getListAdapter().getItem(position);
Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();
}
}
0 komentar:
Posting Komentar