Advertisement
alieneye

Getting Multi-Selected Items From ListActivity

Feb 21st, 2012
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.00 KB | None | 0 0
  1. /* PROBLEM: Trying to get selected items from a multi-select ListActivity and assign to ArrayList 'items' The App runs fine up until the 'button' view is clicked to Toast out the items that were selected from the ListActivity, but app crashes upon call to onClickHandler. */
  2.  
  3. package com.tests.TestCode;
  4.  
  5. import android.app.ListActivity;
  6. import android.os.Bundle;
  7. import java.util.ArrayList;
  8. import android.util.SparseBooleanArray;
  9. import android.view.View;
  10. import android.widget.ArrayAdapter;
  11. import android.widget.Button;
  12. import android.widget.ListView;
  13. import android.widget.Toast;
  14.  
  15. public class MainActivity extends ListActivity {
  16.  
  17. @Override
  18. public void onCreate(Bundle savedInstanceState) {
  19.     super.onCreate(savedInstanceState);
  20.     setContentView(R.layout.main);
  21.  
  22.    final String[] list = {"wrenches","hammers","drills","screwdrivers","saws","chisels","fasteners"};
  23.  
  24.    // Initializing An ArrayAdapter Object for the ListActivity
  25.    final ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.simple_list_item_multiple_choice, list);
  26.    setListAdapter(adapter);
  27.  
  28.    Button button = (Button) findViewById(R.id.button);
  29.    button.setOnClickListener(new View.OnClickListener() {
  30.  
  31.         public void onClick(View v) {
  32.  
  33.             ListView thelistview = (ListView) findViewById(android.R.id.list);
  34.             ArrayList<String> items = null;
  35.  
  36.             SparseBooleanArray checkedItems = thelistview.getCheckedItemPositions();
  37.  
  38.             if (checkedItems != null) {
  39.  
  40.     /********* PROBLEM STARTS HERE **********/
  41.  
  42.                 for (int i=0; i<checkedItems.size(); i++) {
  43.                     if (checkedItems.valueAt(i)) {
  44.                         String item = adapter.getItem(checkedItems.keyAt(i)).toString();
  45.                         items.add(item);
  46.                     }
  47.                 }
  48.                 Toast.makeText(getBaseContext(), items.toString(), Toast.LENGTH_LONG).show();
  49.  
  50.     /********* PROBLEM ENDS HERE **********/
  51.  
  52.             } else {
  53.                 Toast.makeText(getBaseContext(), "checkedItems == null", Toast.LENGTH_LONG).show();
  54.             }
  55.         }
  56.  
  57.      });        
  58.   }
  59. }
  60.  
  61. THE MAIN.XML LAYOUT:
  62.  
  63. <?xml version="1.0" encoding="utf-8"?>
  64. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  65. android:layout_width="fill_parent"
  66. android:layout_height="fill_parent"
  67. android:orientation="vertical" >
  68.  
  69. <TextView
  70.     android:layout_width="fill_parent"
  71.     android:layout_height="wrap_content"
  72.     android:text="@string/hello" />
  73.  
  74. <Button android:id="@+id/button"
  75.     android:layout_width="fill_parent"
  76.     android:layout_height="wrap_content"
  77.     android:text="Selected Items" />    
  78.  
  79. <ListView android:id="@android:id/list" android:layout_width="fill_parent"
  80.     android:layout_height="wrap_content" android:choiceMode="multipleChoice"
  81.     android:textFilterEnabled="true" android:fastScrollEnabled="true" >      
  82. </ListView>
  83.  
  84. </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement