繁体   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);



    }

}       

在此程序中,我正在显示目录结构,以列表形式显示“ sdcard / myfolder”。
现在我要做的是,当我单击“ sdcard / myfolder / anskey.txt” dialog.class活动时,应该打开它。

除了单击“ sdcard / myfolder / anskey.txt”外,没有任何异常,dialog.class活动没有打开。

从我可以看到,您的onListItemClick()签名不正确。 它应该是:

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

这是您要纠正的第一件事。 您的代码可能还有其他问题。

我没有看到您将Listener分配给listView,也没有看到您声明了要实现onListItemClickListener的类,因此onListItemListener的实现只是另一种方法。 按照这个简单的教程来完成

暂无
暂无

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

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