deleteIt
のメソッドでnullポインタ例外が発生しますfilelist.notifyDataSetChanged();
ファイルを削除するのに助けが必要です。親切にしてください。メソッドを追加する方法に数時間かかりましたlongclick
。これは誰かが使用する可能性がありますが、リストからアイテムを削除するのを手伝う必要があります。
public class AndroidExplorer extends ListActivity
{
private List<String> item = null;
private List<String> path = null;
private TextView myPath;
ArrayAdapter filelist = null;
File myFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/GV-Skynet");
String root2 = myFile.getAbsolutePath();;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myPath = (TextView)findViewById(R.id.path);
getDir(root2);
ListView list = getListView();
list.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id)
{
File file = new File(path.get(position));
deleteIt("Do you want to Delete", file.getName(), file );
return true;
}
});
}
private void showToast(String msg)
{
Toast error = Toast.makeText(getBaseContext(), msg, Toast.LENGTH_LONG);
error.show();
}
private void getDir(String dirPath)
{
myPath.setText("Location: " + dirPath);
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File(dirPath);
File[] files = f.listFiles();
if(!dirPath.equals(root2))
{
item.add("Return to GV-Skynet Directory");
path.add(f.getParent());
}
for(int i=0; i < files.length; i++)
{
File file = files[i];
path.add(file.getPath());
if(file.isDirectory())
item.add(file.getName() + "/");
else
item.add(file.getName());
}
ArrayAdapter<String> fileList =
new ArrayAdapter<String>(this, R.layout.row, item);
setListAdapter(fileList);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
File file = new File(path.get(position));
if (file.isDirectory())
{
if(file.canRead())
getDir(path.get(position));
else
{
new AlertDialog.Builder(this)
.setIcon(R.drawable.icon)
.setTitle("[" + file.getName() + "] folder can't be read!")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).show();
}
}
else
{
String fileName = file.getName();
String fname="";
String ext="";
int mid= fileName.lastIndexOf(".");
fname=fileName.substring(0,mid);
ext=fileName.substring(mid+1,fileName.length());
if(ext.equals("jpg"))
{
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(file);
intent.setDataAndType (uri, "image/*");
this.startActivity(intent);
}
if(ext.equals("3gp"))
{
Intent intent = new Intent("android.intent.action.VIEW");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra ("oneshot", 0);
intent.putExtra ("configchange", 0);
Uri uri = Uri.fromFile(file);
intent.setDataAndType (uri, "video/*");
this.startActivity(intent);
}
}
}
public void deleteIt( String title, String name, File file )
{
final String name1 = name;
final File tmFile = file;
new AlertDialog.Builder(this)
.setTitle(title)
.setMessage(name1)
.setPositiveButton("YES", new DialogInterface.OnClickListener() // OnClickListener()
{
public void onClick(DialogInterface arg0, int arg1)
{
//item = new ArrayList<String>();
item.remove(tmFile.getName()); // remove the item
filelist.notifyDataSetChanged(); // let the adapter know to update
showToast("File Deleted: " + tmFile.getName() );
}
})
.setNegativeButton("NO", new DialogInterface.OnClickListener() // OnClickListener()
{
public void onClick(DialogInterface arg0, int arg1) {
// do stuff onclick of CANCEL
showToast("CANCELLING DELETE");
}
}).show();
}
}