Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 尝试仅添加LongListItemClick并获取异常_Android - Fatal编程技术网

Android 尝试仅添加LongListItemClick并获取异常

Android 尝试仅添加LongListItemClick并获取异常,android,Android,我注释掉了我正在添加的受保护布尔onLongListItemClick(ListView l、View v、int position、long id)部分 如果我不发表评论,我会得到一个例外。我正在尝试添加如果有长按,我将删除该文件,这将在以后添加。有没有一个简单的方法可以做到这一点 public class AndroidExplorer extends ListActivity { private List<String> item = null; private Li

我注释掉了我正在添加的受保护布尔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();
        }
    }
}
公共类AndroidExplorer扩展了ListActivity{
私有列表项=null;
私有列表路径=null;
私有文本视图myPath;
File myFile=新文件(Environment.getExternalStorageDirectory().getAbsolutePath()+“/Boot”);
字符串root2=myFile.getAbsolutePath();;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myPath=(TextView)findViewById(R.id.path);
getDir(root2);
}
私有void getDir(字符串dirPath)
{
setText(“位置:“+dirPath”);
item=newarraylist();
路径=新的ArrayList();
文件f=新文件(dirPath);
File[]files=f.listFiles();
如果(!dirPath.equals(root2))
{
添加(“返回引导目录”);
add(f.getParent());
}
对于(int i=0;i
  • 添加一个。看

listView.setOnItemLongClickListener (new OnItemLongClickListener() {
  public boolean onItemLongClick(AdapterView parent, View view, int position, long id) {
    //do your stuff here
  }
});