• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Remove item from the ListView

cbmax

Lurker
I have a ListView

Code:
    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"       
        />
and MainActivity
Code:
package com.example.my;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {
    private Button button;
    private ListView listView;
    private List<String> arrayLangs;
    private ArrayAdapter<String> adapter;
    @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {            
            @Override public void onClick(View arg0) {
                 Toast.makeText(getApplicationContext(),
                             getResources().getString(R.string.hello_world),
                             Toast.LENGTH_SHORT
                         ).show();
            }
        });
        
        listView = (ListView)findViewById(R.id.listView);
        String[] langs = {
            "Java",
            "Groovy",
            "Scala",
            "C",
            "C++",
            "Delphi",
            "Lazarus",
            "Autoit",
            "Python"
        };        
        arrayLangs = java.util.Arrays.<String>asList(langs);
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayLangs);
        listView.setAdapter(adapter);
        
        /*
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {          
                arrayLangs.remove(position);
                adapter.notifyDataSetChanged();
                adapter.notifyDataSetInvalidated();
            }
        });
        */
        
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

                return true;
            }
        });
    }
}
I wanna implement onItemClick or onItemLongClick in order to delete a row from the listView.

I tried the code
Code:
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                arrayLangs.remove(position);
                adapter.notifyDataSetChanged();
            //    adapter.notifyDataSetInvalidated();
            }
        });
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.
 
Back
Top Bottom