1. Are you ready for the Galaxy S20? Here is everything we know so far!

Remove item from the ListView

Discussion in 'Android Development' started by cbmax, Oct 10, 2014.

  1. cbmax

    cbmax Lurker
    Thread Starter

    I have a ListView

    Code (Text):
    1.  
    2.     <ListView
    3.         android:id="@+id/listView"
    4.         android:layout_width="match_parent"
    5.         android:layout_height="wrap_content"      
    6.         />
    7.  
    and MainActivity
    Code (Text):
    1.  
    2. package com.example.my;
    3.  
    4. import java.util.List;
    5.  
    6. import android.app.Activity;
    7. import android.os.Bundle;
    8. import android.view.View;
    9. import android.widget.AdapterView;
    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 Activity {
    16.     private Button button;
    17.     private ListView listView;
    18.     private List<String> arrayLangs;
    19.     private ArrayAdapter<String> adapter;
    20.     @Override protected void onCreate(Bundle savedInstanceState) {
    21.         super.onCreate(savedInstanceState);
    22.         setContentView(R.layout.activity_main);
    23.         button = (Button)findViewById(R.id.button);
    24.         button.setOnClickListener(new View.OnClickListener() {            
    25.             @Override public void onClick(View arg0) {
    26.                  Toast.makeText(getApplicationContext(),
    27.                              getResources().getString(R.string.hello_world),
    28.                              Toast.LENGTH_SHORT
    29.                          ).show();
    30.             }
    31.         });
    32.        
    33.         listView = (ListView)findViewById(R.id.listView);
    34.         String[] langs = {
    35.             "Java",
    36.             "Groovy",
    37.             "Scala",
    38.             "C",
    39.             "C++",
    40.             "Delphi",
    41.             "Lazarus",
    42.             "Autoit",
    43.             "Python"
    44.         };        
    45.         arrayLangs = java.util.Arrays.<String>asList(langs);
    46.         adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayLangs);
    47.         listView.setAdapter(adapter);
    48.        
    49.         /*
    50.         listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    51.             @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {          
    52.                 arrayLangs.remove(position);
    53.                 adapter.notifyDataSetChanged();
    54.                 adapter.notifyDataSetInvalidated();
    55.             }
    56.         });
    57.         */
    58.        
    59.         listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
    60.             @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
    61.  
    62.                 return true;
    63.             }
    64.         });
    65.     }
    66. }
    67.  
    I wanna implement onItemClick or onItemLongClick in order to delete a row from the listView.

    I tried the code
    Code (Text):
    1.  
    2.         listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    3.             @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    4.                 arrayLangs.remove(position);
    5.                 adapter.notifyDataSetChanged();
    6.             //    adapter.notifyDataSetInvalidated();
    7.             }
    8.         });
    9.  
    it raises the error: Unfortunately, {$APP_NAME} has stopped...

    Help me.

    I see a related topic http://androidforums.com/applicatio...simple-listview-layout-sharedpreferences.html

    Dont use SharedPreferences, it posible?

    I tried following android - removing item from ListView on long click - Stack Overflow

    Update: Problem solved. Thank for reading.
     



    1. Download the Forums for Android™ app!


      Download

       
Loading...

Share This Page

Loading...