oadams
oadams

Reputation: 3087

How to delete listview items using a button on the list view items

I want to be able to remove a list item from a ListView by clicking a button that is on the ListView.

So I thought the right path to head down would be to set the OnClickListener for the buttons. The problem is, I don't know how to actually remove the language from AddSpeakerActivity.languages as well as remove the associated item from the ListView. How can I do this? Thanks!

Here is my layout for the list items:

<?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="horizontal" android:baselineAligned="false" >      
    <TextView android:textAppearance="?android:attr/textAppearanceMedium"          
            android:layout_height="wrap_content"                                
            android:text=""                                                     
            android:layout_width="0dp"                                          
            android:layout_weight="1"                                           
            android:layout_gravity="center_vertical"                            
            android:id="@+id/recordingName"/>                                   
    <ImageButton android:src="@drawable/cancel_32"                              
            android:layout_gravity="center"                                     
            android:padding="5dip"                                              
            android:layout_margin="10dip"                                       
            android:scaleType="centerInside"                                    
            android:layout_width="60dip"                                        
            android:layout_height="60dip"                                       
            android:id="@+id/removeButton"/>                                    
</LinearLayout>

Here is the activity in question:

package org.lp20.aikuma.ui;                                                     

import android.app.ListActivity;                                                
import android.content.Intent;                                                  
import android.os.Bundle;                                                       
import android.util.Log;                                                        
import android.view.View;                                                       
import android.widget.ArrayAdapter;                                             
import java.io.IOException;                                                     
import java.util.ArrayList;                                                     
import java.util.List;                                                          
import org.lp20.aikuma.model.Language;                                          
import org.lp20.aikuma.model.Recording;                                         
import org.lp20.aikuma.R;                                                       
import org.lp20.aikuma.util.FileIO;                                             

public class AddSpeakerActivity extends ListActivity {                          

    @Override                                                                   
    public void onCreate(Bundle savedInstanceState) {                           
        super.onCreate(savedInstanceState);                                     
        setContentView(R.layout.add_speaker);                                   
        languages = FileIO.readDefaultLanguages();                              
    }                                                                           

    @Override                                                                   
    public void onResume() {                                                    
        super.onResume();                                                       

        ArrayAdapter<Language> adapter =                                        
                new SpeakerLanguagesArrayAdapter(this, languages);              
        setListAdapter(adapter);                                                
    }                                                                           

    public void onAddLanguageButton(View view) {                                
        Intent intent = new Intent(this, LanguageFilterList.class);             
        startActivityForResult(intent, SELECT_LANGUAGE);                        
    }                                                                           

    protected void onActivityResult(int requestCode, int resultCode, Intent     
            intent) {                                                           
        if (requestCode == SELECT_LANGUAGE) {                                   
            if (resultCode == RESULT_OK) {                                      
                languages.add((Language)                                        
                        intent.getParcelableExtra("language"));                 
                Log.i("langs", "langs");                                        
            }                                                                   
        }                                                                       
    }                                                                           

    static final int SELECT_LANGUAGE = 0;                                       
    private List<Language> languages = new ArrayList<Language>();               
}

Here is the array adapter:

package org.lp20.aikuma.ui;                                                     

import android.content.Context;                                                 
import android.util.Log;                                                        
import android.view.LayoutInflater;                                             
import android.view.View;                                                       
import android.view.View.OnClickListener;                                       
import android.view.ViewGroup;                                                  
import android.widget.ArrayAdapter;                                             
import android.widget.ImageButton;                                              
import android.widget.TextView;                                                 
import java.util.List;                                                          
import org.lp20.aikuma.model.Language;                                          
import org.lp20.aikuma.R;                                                       

public class SpeakerLanguagesArrayAdapter extends ArrayAdapter<Language> {         
    public SpeakerLanguagesArrayAdapter(Context context, List<Language>         
            languages) {                                                        
        super(context, listItemLayout, languages);                              
        inflater = (LayoutInflater)                                             
                context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);         
    }                                                                           

    @Override                                                                   
    public View getView(int position, View _, ViewGroup parent) {               
        Log.i("langs", "getView");                                              
        View recordingView =                                                    
                (View) inflater.inflate(listItemLayout, parent, false);         
        final Language language = getItem(position);                            
        TextView recordingNameView =                                            
                (TextView) recordingView.findViewById(R.id.recordingName);         
        ImageButton removeLanguageButton =                                      
                (ImageButton) recordingView.findViewById(R.id.removeButton);    
        removeLanguageButton.setOnClickListener(new OnClickListener() {         
            public void onClick(View view) {                                    
                Log.i("langs", "language: " + language);                        
            }                                                                   
        });                                                                     

        recordingNameView.setText(language.toString());                         
        return recordingView;                                                   
    }                                                                           

    private static final int listItemLayout =                                   
            R.layout.speakerlanguages_list_item;                                
    private LayoutInflater inflater;                                            

}

Upvotes: 0

Views: 1482

Answers (1)

user1571055
user1571055

Reputation: 369

You need remove item in your list which passed from your activity, and then refresh ui.

removeLanguageButton.setOnClickListener(new OnClickListener() {         
    public void onClick(View view) {                                    
        Log.i("langs", "language: " + language);                        
        languages.remove(language);
        notifyDataSetChange();
        //here you need to notify the activity list to ensure the data is always right.
    }                                                                   
});  

Upvotes: 4

Related Questions