Friday, April 4, 2014

Android ListView onItemClick Event

Here are the output screen of the application where users are able to insert their data through a textbox by clicking ok button as shown in image below. After adding the items users are able to select the items via clicking on it and the selected item is displayed via a toast.
Click Here to download source folder




 

ListExampleActivity.java

 package com.rnd.action;  
 import java.util.ArrayList;  
 import android.app.Activity;  
 import android.os.Bundle;  
 import android.util.Log;  
 import android.util.SparseBooleanArray;  
 import android.view.KeyEvent;  
 import android.view.MotionEvent;  
 import android.view.View;  
 import android.widget.AdapterView;  
 import android.widget.ArrayAdapter;  
 import android.widget.Button;  
 import android.widget.EditText;  
 import android.widget.ListView;  
 import android.widget.Toast;  
 public class ListExampleActivity extends Activity {  
   /** Called when the activity is first created. */  
   ListView lv;  
   String[] outputStrArr;  
   ArrayAdapter<String> aa = null;  
      @Override  
   public void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.main);  
     lv = (ListView) findViewById(R.id.listView_user);  
     final EditText myEditText = (EditText) findViewById(R.id.myeditText);  
     final ArrayList<String> todoItems = new ArrayList<String>();  
     aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,todoItems);  
     lv.setAdapter(aa);  
     myEditText.setOnKeyListener(new View.OnKeyListener() {  
                public boolean onKey(View v, int keyCode, KeyEvent event) {  
                     // TODO Auto-generated method stub  
                     if(event.getAction()== KeyEvent.ACTION_DOWN)  
                     {  
                          if(keyCode == KeyEvent.KEYCODE_DPAD_CENTER)  
                          {  
                               todoItems.add(0,myEditText.getText().toString());  
                               aa.notifyDataSetChanged();  
                               myEditText.setText("");  
                               return true;  
                          }  
                     }  
                     return false;  
                }  
           });  
     lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {  
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,  
                          long arg3) {  
                     //Log.i("Selected Item in list", arg1.toString());  
                     String test = (String) lv.getAdapter().getItem(arg2);  
                     Log.i("Selected Item in list", test);  
                     Toast.makeText(ListExampleActivity.this,test ,Toast.LENGTH_LONG);  
                }  
           });  
     }  
 }  

Main.xml

 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:layout_width="fill_parent"  
   android:layout_height="fill_parent"  
   android:orientation="vertical" >  
   <TextView  
     android:layout_width="fill_parent"  
     android:layout_height="wrap_content"  
     android:text="@string/hello" />  
   <EditText  
     android:id="@+id/myeditText"  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:ems="10" >  
     <requestFocus />  
   </EditText>  
   <ListView  
     android:id="@+id/listView_user"  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:clickable="true"   
      >  
   </ListView>  
 </LinearLayout>  

 

AndroidManifest.xml

 <?xml version="1.0" encoding="utf-8"?>  
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
   package="com.rnd.action"  
   android:versionCode="1"  
   android:versionName="1.0" >  
   <uses-sdk android:minSdkVersion="8" />  
   <application  
     android:icon="@drawable/ic_launcher"  
     android:label="@string/app_name" >  
     <activity  
       android:name=".ListExampleActivity"  
       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 comments:

Post a Comment