2011-04-11 5 views
0

テキストファイルを読み込むアンドロイドアプリケーションを構築しています。 今、私はsdcardに複数のテキストファイルを持っています。私は、ユーザーがファイルのいずれかを選択すると、選択したファイルが読み取られることを望むabc.txt def.txt ghi.txtAndroidのユーザー選択でテキストファイルを動的に読む

:ファイルの 場所は/ SDカード/テキストファイル/
ファイル名です。 私はそのabc.txtのファイルへのパスが定義されている すなわち

File sdcard = Environment.getExternalStorageDirectory(); 
File file = new File(sdcard,pathtofile); 
BufferedReader br = new BufferedReader(new FileReader(file)); 

pathtofile店単一のファイルを読み込むためのコードを知っています。私はpathtofile

答えて

0

また、テキストファイルフォルダ内のすべての項目のリストを作成し、ユーザーが選択できるリストに保存することもできます。

public class DirectoryBrowser extends ListActivity { 

private List<String> items = null; 
private File currentDirectory; 
private ArrayAdapter<String> fileList; 

@Override 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    currentDirectory = new File("/sdcard/textfile"); 
    getFiles(currentDirectory.listFiles()); 

} 

@Override 
protected void onListItemClick(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(); 
     } 
} 

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_text, items); 
    setListAdapter(fileList); 
} 


} 
+0

ありがとうございますekouchiq – ProgramME

0

あなたがしてAlertDialogを使用することができますでそのパスを定義しているよう

私は、ユーザーが現在 を選択したファイルのオブジェクトをファイルにファイルパスを渡すことができますどのような方法がありますが、それはabc.txtのために働きますリスト。

final CharSequence[] items = {"abc.txt", "def.txt", "ghi.txt"}; 

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
builder.setTitle("Pick a file"); 
builder.setItems(items, new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int item) { 
     //Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show(); 

     File sdcard = Environment.getExternalStorageDirectory(); 
     File file = new File(sdcard,items[item]); 
     BufferedReader br = new BufferedReader(new FileReader(file)); 
    } 
}); 
AlertDialog alert = builder.create(); 
関連する問題