2011-04-14 53 views
0
import android.app.Activity; 

import android.app.ListActivity; 

import android.content.ActivityNotFoundException; 

import android.content.Intent; 

import android.net.Uri; 

import android.os.Bundle; 

import android.view.View; 

import android.widget.AdapterView; 

import android.widget.ArrayAdapter; 

import android.widget.ListAdapter; 

import android.widget.ListView; 

import android.widget.TextView; 

import android.widget.Toast; 

import android.widget.AdapterView.OnItemClickListener; 



public class textfile extends ListActivity { 

    // private static final int PICKFILE_RESULT_CODE = 0; 

       private List<String> items = null; 

       private File currentDirectory; 

       private ArrayAdapter<String> fileList; 

        Intent myIntent = null; 

       /** Called when the activity is first created. */ 

    @Override 

    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 

     currentDirectory = new File("/sdcard/myfolder"); 

     getFiles(currentDirectory.listFiles()); 

     setContentView(R.layout.main); 



    } 



    protected void onListItemClick (AdapterView<?> parent, ListView l, View v, int position, long id) 

    { 

    int selectedRow = (int)id; 



     currentDirectory = new File(items.get(selectedRow)); 

    if(currentDirectory.isDirectory()){ 

      getFiles(currentDirectory.listFiles()); 

    } else{ 

        //if the selected file is not a directory. get the filename 

      currentDirectory.getPath(); 

    } 

    Intent myIntent = null; 



    if(((TextView) v).getText().equals("sdcard/myfolder/anskey.txt")){ 

     myIntent = new Intent(v.getContext(), dialog.class); 

     } 





     startActivity(myIntent); 

    } 



    private void getFiles(File[] files){ 

items = new ArrayList<String>(); 

    for(File file : files){ 

    items.add(file.getPath()); 



    } 

     fileList = new ArrayAdapter<String>(this,R.layout.list_item, items); 

     setListAdapter(fileList); 



    } 

}  

在此程序中,我將目錄結構顯示爲「sdcard/myfolder」作爲列表。
現在我想要做的是,當我點擊「SD卡/ myfolder/anskey.txt」dialog.class活動應該打開。在Android上使用TextView問題

沒有例外,但點擊「sdcard/myfolder/anskey.txt」時,dialog.class活動未打開。

+0

當您調用startActivity時,是否運行調試器來驗證myIntent不爲null? – 2011-04-14 05:16:13

+0

它不爲null corey – ProgramME 2011-04-14 05:26:19

+0

我認爲((TextView)v).getText()中存在一些問題,請幫助 – ProgramME 2011-04-14 05:27:36

回答

0

從我所看到的,您的onListItemClick()簽名是不正確的。它應該是:

protected void onListItemClick(ListView l, View v, int position, long id) 

這是您首先要糾正的問題。您的代碼可能存在其他問題。

0

我沒有看到您將偵聽器分配給listView,我沒有看到您聲明您的類也實現了onListItemClickListener,因此onListItemListener的實現只是另一種方法。請按照this simple tutorial完成該操作