Saturday, March 29, 2014

List with checkbox

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 a checkbox and the selected item are only rendered back to list when the button appearing on screen is clicked. 
Android List with checkbox
Fig: Android List with checkbox



 Get Selected Item Using Checkbox in Listview when Button is clicked
Fig:

Get Selected Item Using Checkbox in Listview when Button is clicked



ListCheckboxExampleActivity.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.View;  
 import android.widget.ArrayAdapter;  
 import android.widget.Button;  
 import android.widget.EditText;  
 import android.widget.ListView;  
 public class ListCheckboxExampleActivity 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_multiple_choice,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;  
                }  
           });  
     Button btn_ok = (Button) findViewById(R.id.button_ok);  
     btn_ok.setOnClickListener(new View.OnClickListener() {  
                public void onClick(View v) {  
                     SparseBooleanArray checked = lv.getCheckedItemPositions();  
               ArrayList<String> selectedItems = new ArrayList<String>();  
               for (int i = 0; i < checked.size(); i++) {  
                 // Item position in adapter  
                 int position = checked.keyAt(i);  
                 // Add sport if it is checked i.e.) == TRUE!  
                 if (checked.valueAt(i))  
                   selectedItems.add(aa.getItem(position));        
               }  
               outputStrArr = new String[selectedItems.size()];  
               aa.clear();  
               for (int i = 0; i < selectedItems.size(); i++) {  
                 outputStrArr[i] = selectedItems.get(i);  
                 Log.i("test data" + outputStrArr[i], null, null);  
                 aa.add(selectedItems.get(i));  
               }  
                }  
           });  
   }  
 }  

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:choiceMode="multipleChoice"  
      >  
   </ListView>  
   <Button  
     android:id="@+id/button_ok"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="Button" />  
 </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=".ListCheckboxExampleActivity"  
       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