Most Applications contain list as a major menu listing layout .
What do you need to make a list ?
We are going to make a simple list with some car names and displaythe names of the car selected in a Toast (As I love cars, you can choose your own topic of interest)
Step 1 : Drag a ListView from Composite field in the Palette to the Linear Layout
Step 2 : The code you'll find when you click on activity_main.xml will be :
activity_main.xml
Step 3 : The code for the Java file MainActivity.java in src directory is :
MainActivity.java
Step 4 : Run the project .
When you click on Audi you will get position : 0 and ListItem : Audi
When you click on RollsRoyce you will get Position : 4 and ListItem : 3
Explanation of the code :
What do you need to make a list ?
- A Linear Layout
- A ListView
- String Array
- Array Adapter
- Toast
We are going to make a simple list with some car names and displaythe names of the car selected in a Toast (As I love cars, you can choose your own topic of interest)
Step 1 : Drag a ListView from Composite field in the Palette to the Linear Layout
Step 2 : The code you'll find when you click on activity_main.xml will be :
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/simplelist" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </LinearLayout>You might see a <ListView> tag occuring in the code . It has an id attribute. change it since you have to remember it further . The default might be listView1. I have changed it to simplelist so I can refer it easily later...
Step 3 : The code for the Java file MainActivity.java in src directory is :
MainActivity.java
package com.mia.listproject; import android.os.Bundle; import android.app.Activity; 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 simplelist; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); simplelist = (ListView) findViewById(R.id.list); String[] values = new String[] { "Audi", "BMW", "Mercedes", "RollsRoyce" }; ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, values); simplelist.setAdapter(adapter); simplelist.setOnItemClickListener(new OnItemClickListener() @Override public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) { // TODO Auto-generated method stub\ // ListView Clicked item index int itemPosition = position; // ListView Clicked item value String itemValue = (String) simplelist.getItemAtPosition(position); // Show Alert Toast.makeText(getApplicationContext(), "Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG) .show(); } }); } }
Step 4 : Run the project .
When you click on Audi you will get position : 0 and ListItem : Audi
When you click on RollsRoyce you will get Position : 4 and ListItem : 3
Explanation of the code :
- First we have to make an reference of a predefined class ListView so that we can use it further. For that we import ListView class form widget package.
ListView simplelist;
- We connect the reference simplelist to the ListView in the xml layout by giving the id as the parameter for the function findViewById (Remember the id (bold in the xml code) )
simplelist = (ListView) findViewById(R.id.list);
- Now we make an array of Strings named values which contains the name of our list-items .
String[] values = new String[] { "Audi", "BMW", "Mercedes", "RollsRoyce" };
- We define an object of class ArrayAdapter which is a container for String array. It will tell the compiler that the String values are nothing but the elements of the defined list .
ArrayAdapter<String> adapter = new ArrayAdapter<String(this, android.R.layout.simple_list_item_1, android.R.id.text1, values);
- It takes parameters such as the type of layout we have used(simple_list_item). Since the values in the list are text so text1 and the String array name which is values .
- Then we have to set the adapter by passing object of ArrayAdapter as a parameter to setAdapter function .
simplelist.setAdapter(adapter);
- Then we have to use onItemClick function which defines what should be done when we click the list-item.
simplelist.setOnItemClickListener(new OnItemClickListener() @Override public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
- Then we define an integer which takes the position of list-item from the method parameter named position stores the position of list-item in it.
int itemPosition=position;
- Then we define a String itemValue which defines the value (i.e. BMW or Mercedes ) from the list-item . Specific itemValue is known when we pass the related position to function getItemAtPosition
String itemValue = (String) simplelist.getItemAtPosition(position);
- As the Item in list is clicked we will get a Toast showing the position the value at the position.
Stay Tuned with Made In Android
No comments:
Post a Comment