0

我注释掉了我正在添加受保护的 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();
        }
    }
}
4

1 回答 1

1

.

listView.setOnItemLongClickListener (new OnItemLongClickListener() {
  public boolean onItemLongClick(AdapterView parent, View view, int position, long id) {
    //do your stuff here
  }
});
于 2012-10-17T04:55:39.440 回答