在我的应用程序中,我希望有一个微调器,它由我保存到此应用程序的内部存储器中的不同文件填充。以下,
我怎样才能做到这一点?
答案 0 :(得分:0)
列出目录中的文件并从列表中设置微调器:
String path = getApplicationContext().getFilesDir().toString();
//if you want to list from a specific directory you created
//String path = getApplicationContext().getFilesDir().toString()+"/your_Directory";
File directory = new File(path);
File fileList[] = directory.listFiles();
String[] list = new String[fileList.size()];
String[] spinnerList = new String[fileList.size()];
for (int i=0; i < fileList.length; i++)
{
list[i] = fileList[i].getAbsolutePath().toString();
spinnerList[i] = list[i].substring(list[i].lastIndexOf("/")+1);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, spinnerList);
spinner.setAdapter(adapter);
答案 1 :(得分:0)
我能够使用下面的代码完成它,但现在我想将文件打开到TextView中。 现在我只能在TextView上显示文件的标题,但我想显示文件的内容。 谢谢你的帮助!
public class ListasGravadas extends Activity {
private TextView lista;
private ListView listasGravadas;
private Spinner spinner;
String[] SavedFiles;
private Menu menu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listas_gravadas);
lista = (TextView) findViewById(R.id.lista);
listasGravadas = (ListView) findViewById(R.id.listView);
spinner = (Spinner) findViewById(R.id.spinner);
ShowSavedFiles();
addListenerToSpinner();
FileInputStream fis;
String content = "";
try {
String file = "teste";
fis = openFileInput(file);
byte[] input = new byte[fis.available()];
while (fis.read(input) != -1) {
}
content += new String(input);
lista.setText(content);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
ShowSavedFiles();
}
private void ShowSavedFiles() {
SavedFiles = getApplicationContext().fileList();
ArrayAdapter<String> adapter
= new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
SavedFiles);
spinner.setAdapter(adapter);
}
public void addListenerToSpinner() {
spinner = (Spinner) findViewById(R.id.spinner);
lista = (TextView) findViewById(R.id.lista);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View arg1, int pos, long arg3) {
String result = parent.getItemAtPosition(pos).toString();
lista.setText(result);
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
}
答案 2 :(得分:0)
此代码有效
void ShowSavedFiles() {
SavedFiles = getApplicationContext().fileList();
ArrayAdapter<String> adapter
= new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
SavedFiles);
listSavedFiles.setAdapter(adapter);
adapter.notifyDataSetChanged();