Pages

Tuesday 4 March 2014

Android: Using ListView

In last tutorial we saw how to add click event to buttons and now in this tutorial we will see how to use a listview.

Output will be as:

XML file: res---layout
 <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"  
  >  
 <EditText   
   android:id="@+id/etText"  
   android:layout_width="fill_parent"  
   android:layout_height="wrap_content"  
   android:hint="Enter text"  
   />  
 <Button   
   android:id="@+id/bAdd"  
   android:layout_width="wrap_content"  
   android:layout_height="wrap_content"  
      android:text="Add"    
   />  
 <ListView   
   android:id="@+id/listDisplay"  
   android:layout_width="fill_parent"  
   android:layout_height="wrap_content"  
   >  
 </ListView>  
 </LinearLayout>     

In our layout file we have added one EditText, Button and a ListView.
Clicking a button will take the user data and add it to a ListView.

Java file: src---your package name
 import java.util.ArrayList;  
 import android.os.Bundle;  
 import android.app.Activity;  
 import android.view.Menu;  
 import android.view.View;  
 import android.view.View.OnClickListener;  
 import android.widget.ArrayAdapter;  
 import android.widget.Button;  
 import android.widget.EditText;  
 import android.widget.ListView;  
 public class MainActivity extends Activity implements OnClickListener {  
      Button b1;  
      ListView list;  
      EditText et;  
      private ArrayAdapter<String> adapter;  
      private ArrayList<String> items;  
      @Override  
      protected void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.activity_main);//referencing UI  
           b1=(Button) findViewById(R.id.bAdd); //referencing button  
           list=(ListView) findViewById(R.id.listDisplay); //referencing listview  
           et=(EditText) findViewById(R.id.etText); //referencing edittext  
           b1.setOnClickListener(this); //adding click event to button  
           items=new ArrayList<String>();  
           adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,items);  
           //Adapter used to bind array to the listview  
           list.setAdapter(adapter);//applying adapter to listview  
      }  
      @Override  
      public boolean onCreateOptionsMenu(Menu menu) {  
           // Inflate the menu; this adds items to the action bar if it is present.  
           getMenuInflater().inflate(R.menu.main, menu);  
           return true;  
      }  
      @Override  
      public void onClick(View v) {  
           // TODO Auto-generated method stub  
           switch(v.getId()){  
           case R.id.bAdd:  
                items.add(0, et.getText().toString());//getting data from edittext  
                adapter.notifyDataSetChanged();//refereshes the adapter (listview)  
                et.setText("");  
                break;  
           }  
      }  
 }  
Here we have used an ArrayAdapter which will bind the array(i.e ArrayList) to the ListView.
The onClick method does the job of retrieving user data from EditText and adding it to the ArrayList and the function notifyDataSetChanged() is used to refresh the adapter.

Manifest file:
 <?xml version="1.0" encoding="utf-8"?>  
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
   package="com.example.listvieweg"  
   android:versionCode="1"  
   android:versionName="1.0" >  
   <uses-sdk  
     android:minSdkVersion="8"  
     android:targetSdkVersion="17" />  
   <application  
     android:allowBackup="true"  
     android:icon="@drawable/ic_launcher"  
     android:label="@string/app_name"  
     android:theme="@style/AppTheme" >  
     <activity  
       android:name="com.example.listvieweg.MainActivity"  
       android:label="@string/app_name" >  
       <intent-filter>  
         <action android:name="android.intent.action.MAIN" />  
         <category android:name="android.intent.category.LAUNCHER" />  
       </intent-filter>  
     </activity>  
   </application>  
 </manifest>  
No need to make any changes in a Manifest file.

Test your application and if you have any doubts feel free to ask me in the comments below.

No comments:

Post a Comment