2012-10-17 29 views
0

我註釋掉我加入保護布爾onLongListItemClick(ListView的L,視圖V,INT位置,長ID)的部分。試圖增加onLongListItemClick,卻得到了異常

如果我取消註釋,我會得到一個異常。我試圖添加,如果長時間按下,我將刪除該文件,該文件將在稍後添加。有沒有簡單的方法來做到這一點。

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(); 
     } 
    } 
} 

回答

相關問題