Using TextView on Android Problem
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);
}
}
In this program i am displaying directory structure display "sdcard/myfolder" as a list.
now what i want to do is that when i click on "sdcard/myfolder/anskey.txt" dialog.class activity should open.there is no exc开发者_运维问答eption but on clicking "sdcard/myfolder/anskey.txt" ,the dialog.class activity is not opening.
From what I can see, your onListItemClick()
signature is not right. It should be:
protected void onListItemClick(ListView l, View v, int position, long id)
This is the first thing you will want to correct. There could be other issues with your code.
I didn't see you assigned the Listener to the listView and I didn't see you declared your class to implement onListItemClickListener as well, so the implementation of onListItemListener is just another method. Follow this simple tutorial to accomplish that
精彩评论