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”作为列表 现在我想做的是,当我点击“sdcard / myfolder / anskey.txt”时,dialog.class活动应该打开。
没有例外但是在点击“sdcard / myfolder / anskey.txt”时,dialog.class活动没有打开。
答案 0 :(得分:0)
据我所见,您的onListItemClick()
签名不对。它应该是:
protected void onListItemClick(ListView l, View v, int position, long id)
这是你想要纠正的第一件事。您的代码可能存在其他问题。
答案 1 :(得分:0)
我没有看到你将监听器分配给listView,我也没有看到你声明你的类实现onListItemClickListener,所以onListItemListener的实现只是另一种方法。按照this simple tutorial完成