In this tutorial I will demonstrate you how to make a simple list view using Array Adapter and simple list item which is already present in android run time environment.
Create a new project in eclipse and give it a name and
package name. I am giving name of the project as ListViewDemo and package name as com.listview.listviewdemo.
In activity_main.xml (lies in res => layout) copy
the code shown below :
<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"
>
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/simplelist"
/>
</LinearLayout>
In the above code we defined a ListView in xml file
and give it id as simplelist.
Now in MainActivity.java copy the code shown below :
package com.listview.listviewdemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//find the list view from xml file
listView = (ListView)findViewById(R.id.simplelist);
// make an array which stores the list items
String[] myList = {"List Item1",
"List Item2",
"List Item3",
"List Item4",
"List Item5",
"List Item6",
"List Item7",
"List Item8"};
//array adapter takes 4 parameters, context, layout reference, id of text view, array of items
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, myList);
listView.setAdapter(adapter);
// set the list item on click listener
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
switch(position){
case 0 :
Toast.makeText(MainActivity.this, "Item 1 clicked", Toast.LENGTH_LONG).show();
break;
case 1 :
Toast.makeText(MainActivity.this, "Item 2 clicked", Toast.LENGTH_LONG).show();
break;
case 2 :
Toast.makeText(MainActivity.this, "Item 3 clicked", Toast.LENGTH_LONG).show();
break;
case 3 :
Toast.makeText(MainActivity.this, "Item 4 clicked", Toast.LENGTH_LONG).show();
break;
case 4 :
Toast.makeText(MainActivity.this, "Item 5 clicked", Toast.LENGTH_LONG).show();
break;
case 5 :
Toast.makeText(MainActivity.this, "Item 6 clicked", Toast.LENGTH_LONG).show();
break;
case 6 :
Toast.makeText(MainActivity.this, "Item 7 clicked", Toast.LENGTH_LONG).show();
break;
case 7 :
Toast.makeText(MainActivity.this, "Item 8 clicked", Toast.LENGTH_LONG).show();
break;
}
}
});
}
}
In the MainActivity.java we first bind the ListView
from xml file by using findViewbyId() and than we need to define an Adapter.
Adapter
is used to give the data to the list view, for that make a String array before
which contains all the list items data. Now make an ArrayAdapter which takes 4
parameters in which first parameter is context, second parameter is the
reference to the layout of list item ( in this example we are using android in
build simple list view, so we pass simple list item from android.R.layout), the
third parameter is used to give the id of the text view where each list item
text will be set and in this example we give that id from in built android text
id and the fourth parameter is the string array because we made a ArrayAdapter
which is of type String.
And than just set the adapter in the list view. In
this example we are also set up OnItemClickListener for the list view which
listens whenever any item of the list view will be clicked.
I found best article.thank you for sharing useful info.
ReplyDeletevisit
web programming tutorial
welookups
Those guidelines additionally worked to become a good way to recognize that other people online have the identical fervor like mine to grasp great deal more around this condition.
ReplyDeletepython Training in Bangalore | python Training in Bangalore
Great work done by author of this blog. Its an Amazing blogger. We are one of the best Mobile App Development Company | Mobile App Development Company in Delhi | Mobile App Development Company in India.
ReplyDeleteThank you for posting this blog App Development Company UK
ReplyDeleteIt’s really a nice and helpful piece of information. I’m glad that you shared this helpful info with us. Please keep us informed like this. Thanks for sharing. webprogrammierung berlin
ReplyDeleteThanks for sharing this article. I would like to share this Home Renovations Adelaide
ReplyDelete