Android 从 ListView 和存储中的文件夹中删除文件
Android Delete file from ListView and from a folder in storage
我正在尝试构建一个将从 ListView 中删除文件的应用程序
同时从存储中的文件夹中。
当我运行这个应用程序时,ListView 显示文件夹中的文件。
长按 ListView 中的文件会显示 Toast 消息
但对话框无法显示。该文件未从
ListView 或文件夹。
非常感谢代码帮助解决这个问题。
谢谢。
这是我的代码:
ListView lv = getListView();
lv.setLongClickable(true);
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(final AdapterView<?> arg0, View arg1, final int position, long arg3) {
AlertDialog.Builder alert = new AlertDialog.Builder(FileRemoveActivity.this);
// this Title Fails to display
alert.setTitle("Delete File");
// this Message Fails to display
alert.setMessage("Are you sure you want to delete this file?");
// this Toast 'position' value Does display
Toast.makeText(getApplicationContext(), " " + position, Toast.LENGTH_LONG).show();
alert.setCancelable(false);
// the Yes button Fails to display
alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// compiler warning this code is an Unchecked Cast
ArrayAdapter<String> adapter = (ArrayAdapter<String>) arg0.getAdapter();
// the file at 'position' is Not removed
adapter.remove(adapter.getItem(position));
adapter.notifyDataSetChanged();
}
});
// the Cancel button Fails to display
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
return false;
}
}); // KJP NOTE: Test 07-May-2016 End Test.
在return false;
之前的方法末尾添加alert.create(); alert.show();
。
您的对话框将可见,然后您可以点击 Yes
删除该项目。
我没看到你调用 alert.show() 来实际显示对话框。你建造它,但不展示它。有一个如何工作的例子 here.
我正在尝试构建一个将从 ListView 中删除文件的应用程序 同时从存储中的文件夹中。
当我运行这个应用程序时,ListView 显示文件夹中的文件。 长按 ListView 中的文件会显示 Toast 消息 但对话框无法显示。该文件未从 ListView 或文件夹。
非常感谢代码帮助解决这个问题。
谢谢。
这是我的代码:
ListView lv = getListView();
lv.setLongClickable(true);
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(final AdapterView<?> arg0, View arg1, final int position, long arg3) {
AlertDialog.Builder alert = new AlertDialog.Builder(FileRemoveActivity.this);
// this Title Fails to display
alert.setTitle("Delete File");
// this Message Fails to display
alert.setMessage("Are you sure you want to delete this file?");
// this Toast 'position' value Does display
Toast.makeText(getApplicationContext(), " " + position, Toast.LENGTH_LONG).show();
alert.setCancelable(false);
// the Yes button Fails to display
alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// compiler warning this code is an Unchecked Cast
ArrayAdapter<String> adapter = (ArrayAdapter<String>) arg0.getAdapter();
// the file at 'position' is Not removed
adapter.remove(adapter.getItem(position));
adapter.notifyDataSetChanged();
}
});
// the Cancel button Fails to display
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
return false;
}
}); // KJP NOTE: Test 07-May-2016 End Test.
在return false;
之前的方法末尾添加alert.create(); alert.show();
。
您的对话框将可见,然后您可以点击 Yes
删除该项目。
我没看到你调用 alert.show() 来实际显示对话框。你建造它,但不展示它。有一个如何工作的例子 here.