我有麻煩,使多個刪除工作在Android的ListView。從自定義適配器中刪除項目android
1)我創建了一個CustomAdapter定製有幾個按鈕和文本每行(我把這裏只是我認爲最重要的):
public class PlayableCustomAdapter extends ArrayAdapter<String> {
private final boolean[] checkedItems;
private final LayoutInflater mInflater;
private final ActivityCallback activityCallback;
public PlayableCustomAdapter(Context context, ActivityCallback activityCallback,
List<String> files) {
super(context, NO_SELECTION);
this.activityCallback = activityCallback;
this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.checkedItems = new boolean[files.size()];
addAll(files);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
Holder holder;
if (row == null) {
row = mInflater.inflate(R.layout.list_view_row_song, null);
holder = new Holder();
holder.text = (TextView) row.findViewById(R.id.tvFileName);
holder.configs = (TextView) row.findViewById(R.id.tvFileConfiguration);
holder.checkBox = (CheckBox) row.findViewById(R.id.cbDelete);
holder.playButton = (ImageButton) row.findViewById(R.id.btnPlay);
holder.settingsButton = (ImageButton) row.findViewById(R.id.btnSettings);
holder.text.setText(getItem(position));
holder.configs.setText(getLyricConfiguration(getItem(position)));
row.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
holder.playButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startPrompter(position);
}
});
holder.settingsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startSettings(position);
}
});
holder.text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startEditFile(position);
}
});
holder.checkBox.setChecked(checkedItems[position]);
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
checkedItems[position] = true;
activityCallback.showContent();
} else {
verifySelectedItems(position);
}
}
});
return row;
}
private void verifySelectedItems(int position) {
checkedItems[position] = false;
for (boolean checked : checkedItems) {
if (checked)
return;
}
activityCallback.hideContent();
}
public boolean isChecked(int position) {
return checkedItems[position];
}
@Override
public void remove(String s) {
verifySelectedItems(getPosition(s));
super.remove(s);
notifyDataSetChanged();
}
static class Holder {
TextView text;
TextView configs;
ImageButton playButton;
CheckBox checkBox;
ImageButton settingsButton;
}
2)我用這個適配器在一個Activity中,一切都像一個魅力一樣,除了多次刪除。只有當我清除所有適配器項目時,listview才能正確更新。 當我刪除一個或多個項目時,適配器似乎沒問題。所有職位都正確傳遞。但我不知道如何,只有列表末尾的項目從視圖中刪除。
3)如您所見,我在刪除後使用notifyDataSetChanged();
,問題仍然存在。我可以使其工作的唯一(也是可能)解決方案是在刪除後重新創建活動(recreate();
)。屏幕閃爍,其可怕,但它功能齊全。而現在我有一些空閒時間,我正在問你們一些幫助。
下面是一個使用適配器的活動代碼:
private void deleteFilesFromDisk(List<Integer> positionsToDelete, ArrayAdapter adapter) {
File[] files = getAppFiles();
for (int position : positionsToDelete) {
if (files[position].delete()) {
adapter.remove(fileNames.get(position));
} else
showMessage(R.string.delete_file_error, fileNames.get(position));
}
recreate(); // Horrible workaround, but it works!
}
請記住,這個方法僅僅是一個例子。特別是最後一行。沒有娛樂命令,只有列表末尾的項目被移除(至少在UI中以可視方式)。
任何消耗?
adapter.notifydatasetchanged()?當您調用adapter.remove(fileNames.get(position))而不是繼承的方法 – BIW
您不需要從適配器中刪除項目,從列表中刪除項目並通知適配器數據集已更改 –