Android 如何在listview中单击文本文件时打开它
当我点击文本文件时,我无法打开它。我的程序将转到起始页。我看到很多代码 你应该知道文本文件的确切位置,但是当我不知道文本文件的确切位置时,如何打开文本文件 主要活动打开包含3个按钮的xml当我按下浏览按钮时应用程序打开手机中的所有文件Android 如何在listview中单击文本文件时打开它,android,Android,当我点击文本文件时,我无法打开它。我的程序将转到起始页。我看到很多代码 你应该知道文本文件的确切位置,但是当我不知道文本文件的确切位置时,如何打开文本文件 主要活动打开包含3个按钮的xml当我按下浏览按钮时应用程序打开手机中的所有文件 package com.example.fileexplorerb; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import
package com.example.fileexplorerb;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener{
private static final int REQUEST_PATH = 1;
Button browseButton;
Button exitButton;
Button nextButton;
String curFileName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
browseButton = (Button)findViewById(R.id.button1);
browseButton.setOnClickListener(this);
exitButton = (Button)findViewById(R.id.button2);
exitButton.setOnClickListener(this);
nextButton= (Button)findViewById(R.id.button3);
nextButton.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
// See which child activity is calling us back.
if (requestCode == REQUEST_PATH){
if (resultCode == RESULT_OK) {
curFileName = data.getStringExtra("GetFileName");
//edittext.setText(curFileName);
}
}
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.button1){
Intent intent1 = new Intent(this, FileChooser.class);
startActivityForResult(intent1,REQUEST_PATH);
}
if (v.getId() == R.id.button2){
finish();
}
if (v.getId() == R.id.button3){
//Intent intent2 = new Intent(this, FileChooser.class);
//startActivityForResult(intent2,REQUEST_PATH);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setType("application/txt");
startActivity(intent);
}
}
}
File ArrayAdapter显示文件大小、创建日期和名称
package com.example.fileexplorerb;
import java.util.List;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class FileArrayAdapter extends ArrayAdapter<Item>{
private Context c;
private int id;
private List<Item>items;
public FileArrayAdapter(Context context, int textViewResourceId,
List<Item> objects) {
super(context, textViewResourceId, objects);
c = context;
id = textViewResourceId;
items = objects;
}
public Item getItem(int i)
{
return items.get(i);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(id, null);
}
/* create a new view of my layout and inflate it in the row */
//convertView = ( RelativeLayout ) inflater.inflate( resource, null );
final Item o = items.get(position);
if (o != null) {
TextView t1 = (TextView) v.findViewById(R.id.textView1);
TextView t2 = (TextView) v.findViewById(R.id.textView2);
TextView t3 = (TextView) v.findViewById(R.id.textView3);
/* Take the ImageView from layout and set the city's image */
ImageView imageCity = (ImageView) v.findViewById(R.id.imageView1);
String uri = "drawable/" + o.getImage();
int imageResource = c.getResources().getIdentifier(uri, null, c.getPackageName());
Drawable image = c.getResources().getDrawable(imageResource);
imageCity.setImageDrawable(image);
if(t1!=null)
t1.setText(o.getName());
if(t2!=null)
t2.setText(o.getData());
if(t3!=null)
t3.setText(o.getDate());
}
return v;
}
}
package com.example.fileexplorerb;
导入java.util.List;
导入android.content.Context;
导入android.graphics.drawable.drawable;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ArrayAdapter;
导入android.widget.ImageView;
导入android.widget.TextView;
公共类FileArrayAdapter扩展了ArrayAdapter{
私有上下文c;
私有int-id;
私人物品清单;
公共文件ArrayAdapter(上下文,int textViewResourceId,
列出对象){
超级(上下文、textViewResourceId、对象);
c=上下文;
id=textViewResourceId;
项目=对象;
}
公共项getItem(int i)
{
返回项目。获取(i);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图v=转换视图;
如果(v==null){
LayoutInflater vi=(LayoutInflater)c.getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
v=vi.充气(id,空);
}
/*创建我的布局的新视图并在行中充气*/
//convertView=(RelativeLayout)充气器。充气(资源,空);
最终项目o=项目。获取(位置);
如果(o!=null){
TextView t1=(TextView)v.findViewById(R.id.textView1);
TextView t2=(TextView)v.findViewById(R.id.textView2);
TextView t3=(TextView)v.findViewById(R.id.textView3);
/*从布局中获取ImageView并设置城市形象*/
ImageView imageCity=(ImageView)v.findViewById(R.id.imageView1);
字符串uri=“drawable/”+o.getImage();
int-imageResource=c.getResources().getIdentifier(uri,null,c.getPackageName());
Drawable image=c.getResources().getDrawable(imageResource);
imageCity.setImageDrawable(图像);
如果(t1!=null)
t1.setText(o.getName());
如果(t2!=null)
t2.setText(o.getData());
如果(t3!=null)
t3.setText(o.getDate());
}
返回v;
}
}
文件选择器将图像设置为文件和
package com.example.fileexplorerb;
import java.io.File;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.text.DateFormat;
import android.os.Bundle;
import android.app.ListActivity;
import android.content.Intent;
import android.view.View;
import android.widget.ListView;
public class FileChooser extends ListActivity {
private File currentDir;
private FileArrayAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
currentDir = new File("/sdcard/");
fill(currentDir);
}
private void fill(File f)
{
File[]dirs = f.listFiles();
this.setTitle("Current Dir: "+f.getName());
List<Item>dir = new ArrayList<Item>();
List<Item>fls = new ArrayList<Item>();
try{
for(File ff: dirs)
{
Date lastModDate = new Date(ff.lastModified());
DateFormat formater = DateFormat.getDateTimeInstance();
String date_modify = formater.format(lastModDate);
if(ff.isDirectory()){
File[] fbuf = ff.listFiles();
int buf = 0;
if(fbuf != null){
buf = fbuf.length;
}
else buf = 0;
String num_item = String.valueOf(buf);
if(buf == 0) num_item = num_item + " item";
else num_item = num_item + " items";
//String formated = lastModDate.toString();
dir.add(new Item(ff.getName(),num_item,date_modify,ff.getAbsolutePath(),"directory_icon"));
}
else
{
fls.add(new Item(ff.getName(),ff.length() + " Byte", date_modify, ff.getAbsolutePath(),"file_icon"));
}
}
}catch(Exception e){
}
Collections.sort(dir);
Collections.sort(fls);
dir.addAll(fls);
if(!f.getName().equalsIgnoreCase("sdcard"))
dir.add(0,new Item("..","Parent Directory","",f.getParent(),"directory_up"));
adapter = new FileArrayAdapter(FileChooser.this,R.layout.file_view,dir);
this.setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Item o = adapter.getItem(position);
if(o.getImage().equalsIgnoreCase("directory_icon")||o.getImage().equalsIgnoreCase("directory_up")){
currentDir = new File(o.getPath());
fill(currentDir);
}
else
{
onFileClick(o);
}
}
private void onFileClick(Item o)
{
//Toast.makeText(this, "Folder Clicked: "+ currentDir, Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
intent.putExtra("GetPath",currentDir.toString());
intent.putExtra("GetFileName",o.getName());
setResult(RESULT_OK, intent);
finish();
}
}
XML for main activity shows main menu when you open application
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="142dp"
android:text="Browse" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_alignTop="@+id/button1"
android:layout_marginTop="63dp"
android:text="Exit" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button2"
android:layout_below="@+id/button2"
android:layout_marginTop="14dp"
android:text="Next" />
</RelativeLayout>
package com.example.fileexplorerb;
导入java.io.File;
导入java.sql.Date;
导入java.util.ArrayList;
导入java.util.Collections;
导入java.util.List;
导入java.text.DateFormat;
导入android.os.Bundle;
导入android.app.ListActivity;
导入android.content.Intent;
导入android.view.view;
导入android.widget.ListView;
公共类FileChooser扩展了ListActivity{
私有文件currentDir;
专用文件阵列适配器;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
currentDir=新文件(“/sdcard/”);
填充(currentDir);
}
专用空白填充(文件f)
{
File[]dirs=f.listFiles();
这个.setTitle(“当前目录:+f.getName());
Listdir=newarraylist();
Listfls=newarraylist();
试一试{
用于(文件ff:dirs)
{
Date lastModDate=新日期(ff.lastModified());
DateFormat Formatter=DateFormat.getDateTimeInstance();
字符串日期\u modify=formatter.format(lastModDate);
if(ff.isDirectory()){
文件[]fbuf=ff.listFiles();
int-buf=0;
如果(fbuf!=null){
buf=fbuf.长度;
}
否则buf=0;
String num_item=String.valueOf(buf);
如果(buf==0)num_item=num_item+“item”;
else num_item=num_item+“items”;
//格式化字符串=lastModDate.toString();
目录添加(新项(ff.getName(),num_项,日期_修改,ff.getAbsolutePath(),“目录图标”);
}
其他的
{
fls.add(新项(ff.getName(),ff.length()+“Byte”,日期修改,ff.getAbsolutePath(),“文件图标”);
}
}
}捕获(例外e){
}
集合排序(dir);
集合。排序(fls);
addAll主任(fls);
如果(!f.getName().equalsIgnoreCase(“SD卡”))
目录添加(0,新项(“…”,“父目录”,“”,f.getParent(),“目录更新”);
adapter=newfilearrayadapter(FileChooser.this,R.layout.file\u view,dir);
这个.setListAdapter(适配器);
}
@凌驾
受保护的void onListItemClick(列表视图l、视图v、整数位置、长id){
//TODO自动生成的方法存根
super.onListItemClick(左、右、位置、id);
项目o=适配器。获取项目(位置);
如果(o.getImage().equalsIgnoreCase(“目录图标”)| | o.getImage().equalsIgnoreCase(“目录图标”)){
currentDir=新文件(o.getPath());
填充(currentDir);
}
其他的
{
onFileClick(o);
}
}
私有void onFileClick(o项)
{
//Toast.makeText(单击此文件夹:“+currentDir,Toast.LENGTH_SHORT).show();
意图=新意图();
intent.putExtra(“GetPath”,currentDir.toString());
file view
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="50dip"
android:layout_height="50dip" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView2"
android:layout_marginLeft="36dp"
android:layout_toRightOf="@+id/imageView1"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView1"
android:layout_alignLeft="@+id/textView1"
android:text="TextView" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_alignParentRight="true"
android:layout_marginRight="19dp"
android:text="TextView" />
</RelativeLayout>