我注释掉了我正在添加protected boolean onLongListItemClick(ListView l,View v,int position,long id)的部分。
如果我取消评论,我会收到例外情况。我想添加如果有长按我将删除该文件,稍后将添加。有没有一种简单的方法可以做到这一点。
public class AndroidExplorer extends ListActivity {
private List<String> item = null;
private List<String> path = null;
private TextView myPath;
File myFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Boot");
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);
}
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 Boot 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);
}
// protected boolean onLongListItemClick(ListView l, View v, int position, long id) {
// // Log.i(TAG, "onLongListItemClick id=" + id);
// Toast.makeText(this,
// "I will be terminated",
// Toast.LENGTH_LONG).show();
// return true;
// }
@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"))
{
Toast.makeText(this,
"view the jpg",
Toast.LENGTH_LONG).show();
}
if(ext.equals("3gp"))
{
Toast.makeText(this,
"play the video",
Toast.LENGTH_LONG).show();
}
new AlertDialog.Builder(this)
.setIcon(R.drawable.icon)
.setTitle("[" + file.getName() + "]")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).show();
}
}
}
答案 0 :(得分:1)
listView.setOnItemLongClickListener (new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView parent, View view, int position, long id) {
//do your stuff here
}
});