0

我希望能够通过单击 ListView 上的按钮从 ListView 中删除列表项。

所以我认为正确的方法是为按钮设置 OnClickListener。问题是,我不知道如何从 AddSpeakerActivity.languages 中实际删除语言以及从 ListView 中删除相关项目。我怎样才能做到这一点?谢谢!

这是我的列表项布局:

<?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>

这是有问题的活动:

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>();               
}

这是数组适配器:

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;                                            

}
4

1 回答 1

4

您需要从您的活动中删除列表中的项目,然后刷新用户界面。

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.
    }                                                                   
});  
于 2013-07-19T07:08:51.020 回答