简体   繁体   English

在Android上使用TextView问题

[英]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. 在此程序中,我正在显示目录结构,以列表形式显示“ sdcard / myfolder”。
now what i want to do is that when i click on "sdcard/myfolder/anskey.txt" dialog.class activity should open. 现在我要做的是,当我单击“ sdcard / myfolder / anskey.txt” dialog.class活动时,应该打开它。

there is no exception but on clicking "sdcard/myfolder/anskey.txt" ,the dialog.class activity is not opening. 除了单击“ sdcard / myfolder / anskey.txt”外,没有任何异常,dialog.class活动没有打开。

From what I can see, your onListItemClick() signature is not right. 从我可以看到,您的onListItemClick()签名不正确。 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. 我没有看到您将Listener分配给listView,也没有看到您声明了要实现onListItemClickListener的类,因此onListItemListener的实现只是另一种方法。 Follow this simple tutorial to accomplish that 按照这个简单的教程来完成

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM