我希望能夠通過單擊列表視圖上的按鈕從列表視圖中移除列表項目。如何使用列表視圖中的按鈕刪除列表視圖項目
所以我認爲正確的道路是爲按鈕設置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;
}
定義一個按鈕單擊偵聽器,然後刪除底層數據。在適配器上使用'notifyDataSetChanged'更新/刷新列表視圖。 – Raghunandan