我在外部存储的文件夹中有一个wav文件列表。我用ListView显示这个列表,这是我的代码:
public class MainActivity extends Activity {
final StringBuffer stringbuff = new StringBuffer();
private ListView mainListView ;
private ArrayAdapter<String> listAdapter ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainListView = (ListView) findViewById( R.id.mainListView );
ArrayList<String> List = new ArrayList<String>();
File storage = Environment.getExternalStorageDirectory();
File file = new File(storage,"/folder/");
if (file.exists() && file.isDirectory()) {
for (String s : file.list()) {
stringbuffer.append(s + " ");
List.addAll( Arrays.asList(s) );
}
}
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, R.id.rowTextView,List);
mainListView.setAdapter( listAdapter );
}
我希望点击按钮选择文件的路径。我该怎么办?
答案 0 :(得分:0)
以下代码将显示带有Toast的文件路径
mainListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
String filepath = new File(storage + "/folder/"+ List.get(arg2)).getAbsolutePath();
Toast.makeText(getApplicationContext(), filepath, Toast.LENGTH_LONG).show();
}
});
答案 1 :(得分:0)
您需要添加OnItemClickListener
AdapterView.OnItemClickListener openSelection = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
String p = (String) arg0.getItemAtPosition(arg2);
Toast.makeText(UploadFilePage.this, p, Toast.LENGTH_LONG).show();
}
};
mainListView.setOnItemClickListener(openSelection);