package com.paad.trial;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class AndroindApplication extends ListActivity {
private List<String> item = null;
static public List<String> path = null;
private String root="/";
private TextView myPath;
static public int pathh;
private Object sdcardEnvironment;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sdcard);
myPath = (TextView)findViewById(R.id.path);
getDir("/sdcard/");
}
private void getDir(String dirPath)
{
myPath.setText("Location: " + dirPath);
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File(dirPath);
File[] files = f.listFiles();
if(!dirPath.equals(root))
{
item.add(root);
path.add(root);
item.add("../");
path.add(f.getParent());
}
for(int i=0; i < files.length; i++)
{
File file = files[i];
path.add(file.getPath());
if(file.isDirectory())
item.add(file.getName() + "/");
else
item.add(file.getName());
}
ArrayAdapter<String> fileList =
new ArrayAdapter<String>(this, R.layout.row, item);
setListAdapter(fileList);
}
@Override
protected void onListItemClick(ListView l, View view, int position, long id) {
File file = new File(path.get(position));
pathh=position;
// Log.e("path="+path,"dxgx");
if (file.isDirectory())
{
if(file.canRead())
getDir(path.get(position));
else
{
new AlertDialog.Builder(this)
.setIcon(R.drawable.icon)
.setTitle("[" + file.getName() + "] folder can't be read!")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).show();
}
}
else
{
Intent myIntent = new Intent(view.getContext(), Readfile.class);
startActivityForResult(myIntent, 0);
Bundle b=new Bundle();
// Bundle
//myIntent.p
/* new AlertDialog.Builder(this)
.setIcon(R.drawable.icon)
.setTitle("[" + file.getName() + "]")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).show();*/
}
}
}
package com.paad.trial;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Readfile extends Activity {
/** Called when the activity is first created. */
AndroindApplication ob=new AndroindApplication();
Intent i;
String[] arr=null;
private Button pButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.text);
//File sdcard = Environment.getExternalStorageDirectory();
String myPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/myfolder/";
//Get the text file
File file = new File(myPath + AndroindApplication.path.get(AndroindApplication.pathh));
//ob.pathh
//Read text from file
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line=null;
//int i=0;
List<String> lines = new ArrayList<String>();
while ((line = br.readLine()) != null) {
lines.add(line);
// arr[i]=line;
// i++;
text.append(line);
text.append('\n');
}
arr = lines.toArray(new String[lines.size()]);
}
catch (IOException e) {
//You'll need to add proper error handling here
}
//Find the view by its id
TextView tv = (TextView)findViewById(R.id.text_view);
//Set the text
tv.setText(text);
pButton = (Button) findViewById(R.id.Button01);
pButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Bundle b=new Bundle();
b.putStringArray("strings", arr);
i=new Intent(v.getContext(),texttospeech.class);
i.putExtras(b);
startActivityForResult(i, 0);
}
});
}
}
我的sdcard包含两个文本文件。我想当用户在应用程序中选择其中一个然后它应该打开所选文件。 在上面的代码中,我试图将AndroindApplication.java中的变量路径postion传递给Readfile.java。但是在Readfile中它无法获取路径。
文本文件存储在sdcard下的myfolder中。 readfile.java中的File Oject使用这两个变量来读取文件。
HELP
答案 0 :(得分:0)
在Readfile
中,您没有 - 也不应该 - 实例化AndroindApplication
。要在两个活动之间传递字符串,您应该使用Intents。
在字符串的情况下,它非常简单。见Passing data between activities in Android