0

so I have a listview and I want that when I click a button, it does remove the selected item from the listview.My listview is defined like this in xml file, and thats what make it selectable:

<ListView
        android:id="@+id/listView1"
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:layout_alignLeft="@+id/button2"
        android:layout_below="@+id/textView2"
        android:choiceMode="singleChoice"
        android:background="@drawable/bg_key" > >
    </ListView>

So far I have this java code with no errors, but my app crash when I launch it:

public class MainActivity extends ListActivity {

    Button button1;
    Button button3;
    Button button2;
    Button button4;
    ListView listView1;
    ListView listView2;
    Spinner s1;
    String text;
    Integer i;
    SparseBooleanArray checkedItemPositions ;


    ArrayAdapter<String> adapter;
    ArrayAdapter<String> adapter2;
    int itemPos;
    ArrayList<String> list = new ArrayList<String>();
    ArrayList<String> list2 = new ArrayList<String>();

     @Override
        protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            button1 = (Button) findViewById(R.id.button1);
            button3 = (Button) findViewById(R.id.button3); 
            button4 = (Button) findViewById(R.id.button4); 
            button2 = (Button) findViewById(R.id.button2);
            s1 = (Spinner) findViewById(R.id.spinner1);
            listView1 = (ListView) findViewById(R.id.listView1);
            listView2 = (ListView) findViewById(R.id.listView2);



            adapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_single_choice, list);
                listView1.setAdapter(adapter);

                button2.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View v) {

                        String text = s1.getSelectedItem().toString();
                        list.add(text); 
                        adapter.notifyDataSetChanged();



                    }});

                adapter2 = new ArrayAdapter<String>(this,
                        android.R.layout.simple_list_item_single_choice, list2);
                    listView2.setAdapter(adapter2);

                    button1.setOnClickListener(new View.OnClickListener() {

                        public void onClick(View v) {

                            String text = s1.getSelectedItem().toString();
                            list2.add(text); 
                            adapter2.notifyDataSetChanged();



                        }});

                    button3.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            /** Getting the checked items from the listview */
                            SparseBooleanArray checkedItemPositions = getListView().getCheckedItemPositions();


                            for(int i=itemPos-1; i >= 0; i--){
                                if(checkedItemPositions.get(i)){
                                    adapter.remove(list.get(i));
                                }
                            }
                            checkedItemPositions.clear();
                            adapter.notifyDataSetChanged();
                        }
                    });



     listView1.setOnItemClickListener(new OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {

            view.setSelected(true);

        itemPos = position;

      }});
    listView2.setOnItemClickListener(new OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {

                view.setSelected(true);

            itemPos = position;

          }
    });}}

logcat

11-03 13:58:28.496: E/Trace(624): error opening trace file: No such file or directory (2)
11-03 13:58:28.776: D/dalvikvm(624): GC_FOR_ALLOC freed 125K, 3% free 6156K/6343K, paused 41ms, total 43ms
11-03 13:58:28.866: D/dalvikvm(624): GC_FOR_ALLOC freed 1K, 4% free 6498K/6727K, paused 23ms, total 23ms
11-03 13:58:28.926: D/dalvikvm(624): GC_FOR_ALLOC freed 343K, 9% free 6309K/6919K, paused 48ms, total 48ms
11-03 13:58:28.936: I/dalvikvm-heap(624): Grow heap (frag case) to 7.085MB for 921616-byte allocation
11-03 13:58:29.006: D/dalvikvm(624): GC_CONCURRENT freed <1K, 9% free 7209K/7879K, paused 32ms+3ms, total 69ms
11-03 13:58:29.156: D/dalvikvm(624): GC_FOR_ALLOC freed 0K, 9% free 7209K/7879K, paused 28ms, total 28ms
11-03 13:58:29.166: I/dalvikvm-heap(624): Grow heap (frag case) to 7.476MB for 409936-byte allocation
11-03 13:58:29.216: D/dalvikvm(624): GC_CONCURRENT freed 0K, 9% free 7609K/8327K, paused 25ms+4ms, total 50ms
11-03 13:58:29.216: D/dalvikvm(624): WAIT_FOR_CONCURRENT_GC blocked 25ms
11-03 13:58:29.416: D/gralloc_goldfish(624): Emulator without GPU emulation detected.
11-03 14:00:42.286: I/Choreographer(624): Skipped 162 frames!  The application may be doing too much work on its main thread.
11-03 14:00:42.656: I/Choreographer(624): Skipped 44 frames!  The application may be doing too much work on its main thread.
11-03 14:03:55.266: E/Trace(672): error opening trace file: No such file or directory (2)
11-03 14:03:56.456: D/dalvikvm(672): GC_FOR_ALLOC freed 128K, 3% free 6156K/6343K, paused 35ms, total 36ms
11-03 14:03:56.546: D/dalvikvm(672): GC_FOR_ALLOC freed 1K, 4% free 6498K/6727K, paused 24ms, total 24ms
11-03 14:03:56.576: D/dalvikvm(672): GC_FOR_ALLOC freed 343K, 9% free 6309K/6919K, paused 24ms, total 24ms
11-03 14:03:56.586: I/dalvikvm-heap(672): Grow heap (frag case) to 7.085MB for 921616-byte allocation
11-03 14:03:56.656: D/dalvikvm(672): GC_CONCURRENT freed <1K, 9% free 7209K/7879K, paused 22ms+14ms, total 65ms
11-03 14:03:56.776: D/dalvikvm(672): GC_FOR_ALLOC freed 0K, 9% free 7209K/7879K, paused 22ms, total 22ms
11-03 14:03:56.776: I/dalvikvm-heap(672): Grow heap (frag case) to 7.476MB for 409936-byte allocation
11-03 14:03:56.826: D/dalvikvm(672): GC_CONCURRENT freed 0K, 9% free 7609K/8327K, paused 15ms+4ms, total 47ms
11-03 14:03:56.826: D/dalvikvm(672): WAIT_FOR_CONCURRENT_GC blocked 15ms
11-03 14:03:57.046: D/gralloc_goldfish(672): Emulator without GPU emulation detected.
11-03 14:03:57.516: I/Choreographer(672): Skipped 42 frames!  The application may be doing too much work on its main thread.
11-03 14:52:41.686: E/Trace(720): error opening trace file: No such file or directory (2)
11-03 14:52:42.576: D/dalvikvm(720): GC_FOR_ALLOC freed 75K, 3% free 6147K/6275K, paused 41ms, total 42ms
11-03 14:52:42.666: D/dalvikvm(720): GC_FOR_ALLOC freed 1K, 3% free 6490K/6663K, paused 28ms, total 28ms
11-03 14:52:42.716: D/dalvikvm(720): GC_FOR_ALLOC freed 343K, 9% free 6300K/6855K, paused 24ms, total 24ms
11-03 14:52:42.726: I/dalvikvm-heap(720): Grow heap (frag case) to 7.078MB for 921616-byte allocation
11-03 14:52:42.806: D/dalvikvm(720): GC_CONCURRENT freed <1K, 8% free 7200K/7815K, paused 23ms+24ms, total 78ms
11-03 14:52:42.926: D/dalvikvm(720): GC_FOR_ALLOC freed 0K, 8% free 7200K/7815K, paused 22ms, total 22ms
11-03 14:52:42.926: I/dalvikvm-heap(720): Grow heap (frag case) to 7.468MB for 409936-byte allocation
11-03 14:52:42.976: D/dalvikvm(720): GC_CONCURRENT freed 0K, 9% free 7601K/8263K, paused 14ms+4ms, total 48ms
11-03 14:52:42.976: D/dalvikvm(720): WAIT_FOR_CONCURRENT_GC blocked 14ms
11-03 14:52:43.017: D/AndroidRuntime(720): Shutting down VM
11-03 14:52:43.017: W/dalvikvm(720): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
11-03 14:52:43.036: E/AndroidRuntime(720): FATAL EXCEPTION: main
11-03 14:52:43.036: E/AndroidRuntime(720): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.devoir3/com.example.devoir3.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
11-03 14:52:43.036: E/AndroidRuntime(720):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
11-03 14:52:43.036: E/AndroidRuntime(720):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-03 14:52:43.036: E/AndroidRuntime(720):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-03 14:52:43.036: E/AndroidRuntime(720):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-03 14:52:43.036: E/AndroidRuntime(720):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-03 14:52:43.036: E/AndroidRuntime(720):  at android.os.Looper.loop(Looper.java:137)
11-03 14:52:43.036: E/AndroidRuntime(720):  at android.app.ActivityThread.main(ActivityThread.java:4745)
11-03 14:52:43.036: E/AndroidRuntime(720):  at java.lang.reflect.Method.invokeNative(Native Method)
11-03 14:52:43.036: E/AndroidRuntime(720):  at java.lang.reflect.Method.invoke(Method.java:511)
11-03 14:52:43.036: E/AndroidRuntime(720):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-03 14:52:43.036: E/AndroidRuntime(720):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-03 14:52:43.036: E/AndroidRuntime(720):  at dalvik.system.NativeStart.main(Native Method)
11-03 14:52:43.036: E/AndroidRuntime(720): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
11-03 14:52:43.036: E/AndroidRuntime(720):  at android.app.ListActivity.onContentChanged(ListActivity.java:243)
11-03 14:52:43.036: E/AndroidRuntime(720):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:259)
11-03 14:52:43.036: E/AndroidRuntime(720):  at android.app.Activity.setContentView(Activity.java:1867)
11-03 14:52:43.036: E/AndroidRuntime(720):  at com.example.devoir3.MainActivity.onCreate(MainActivity.java:38)
11-03 14:52:43.036: E/AndroidRuntime(720):  at android.app.Activity.performCreate(Activity.java:5008)
11-03 14:52:43.036: E/AndroidRuntime(720):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-03 14:52:43.036: E/AndroidRuntime(720):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
11-03 14:52:43.036: E/AndroidRuntime(720):  ... 11 more

Im not quite sur on how to remove item from my listview on click

thank you

Phil_oneil
  • 267
  • 3
  • 11

1 Answers1

1

Your Activity extends ListActivity, if you extend from Activity you should have no problem

rickyalbert
  • 2,552
  • 4
  • 21
  • 31
  • I did this and then I get error The method getListView() is undefined for the type new View.OnClickListener(){} – Phil_oneil Nov 03 '14 at 21:31
  • And that is correct, but where are you using this method? – rickyalbert Nov 03 '14 at 21:50
  • I'll try to explain better: ListActivity is a default android activity that contains only one ListView, and it has the method "getListView()" if you want to get a reference to it. In your activity you have buttons and other stuff so you have to create your own activity. Replace "getListView()" with "listView1" or "listView2" (I don't know what you need there). – rickyalbert Nov 03 '14 at 21:58
  • thanks, that was the problem. However, when I launch the app and click the delete button on an item, it just unmark it and it doesnt remove it from list – Phil_oneil Nov 03 '14 at 22:54