カスタムSimpleCursorAdapterを使用してリストビューを作成しました。リストビューのonItemClickListenerで、別のアクティビティ、つまりその作業を開きます。今onitemLongClickListener私は、はい、ファイルを削除するための選択肢なしで警告ダイアログボックスを表示する。はいを選択すると、私はカーソルを取得し、項目の関連するデータベースエントリを取得し、そのエントリを削除し、アプリケーションデータディレクトリからもファイルを削除するが、私はアプリケーションを閉じて、それを開くまでlistviewは自動的に更新されません。私はlongclickリスナーでファイルとdbエントリを削除した後にadapter.changeCursor(カーソル)を使用しようとしましたが、成功しませんでした。ファイルとデータベースのエントリを削除した直後に、それを直ちに更新するには何が必要ですか?長いクリックでアイテムを削除した後で、リストビューが更新されませんか?
MainActivity:あなたが呼び出す必要が
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, final int position, long l) {
cursor = (Cursor) lv.getAdapter().getItem(position);
filepath = cursor.getString(cursor.getColumnIndex("filename"));
final File file = new File(filepath);
builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Delete File");
builder.setMessage("Are you sure you want to delete this file?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
boolean isDeleted = file.delete();
int count = db.delteSingleEntry(filepath);
if (isDeleted && count != 0){
Toast.makeText(MainActivity.this, "File deleted and Database entry removed", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT).show();
}
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
adapter.changeCursor(cursor);
return false;
}
});
}
ランダムaspirer、私は()alert.show後の()とあれば後adapter.notifydatasetchanged入れてみました(isDeleted &&カウント!= 0){しかし、それは動作しません。私のコードを見て、私に教えてください。adapter.notifydatasetchanged()ステートメントをどこに置くべきですか? –
私はそれを働かせました。 delete文の後にpopulatelistview()メソッドを呼び出し、リストビューをimmedietlyに更新しました。 Thanx random aspirer。 –