我使用ApacheURLLister
来查看mp3文件列表。我只需要显示文件名,但ApacheURLLister
显示完整路径,如"http://server.com/a/song.mp3"。我只需要显示"song.mp3"。
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.listAudio);
new getAudiofromServer().execute();
new downloadAudioFromServer().execute();
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String url = null;
Object o = myList.get(position);
url = o.toString().replace(" ", "%20").trim();
play = (PlaySongAsy) new PlaySongAsy(url).execute();
}
});
}
private void playSong(String songPath) {
try {
mp.reset();
mp.setDataSource(songPath);
mp.prepare();
mp.start();
} catch (IOException e) {
Log.v(getString(R.string.app_name), e.getMessage());
}
}
private class downloadAudioFromServer extends
AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... url) {
int count;
try {
URL url1 = new URL("http://server/Mora/");
URLConnection conexion = url1.openConnection();
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
InputStream input = new BufferedInputStream(url1.openStream());
OutputStream output = new FileOutputStream(
Environment.getExternalStorageDirectory() + "/Sounds/");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
publishProgress((int) (total * 100 / lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
}
return null;
}
}
class getAudiofromServer extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Getting File list from server, Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
@SuppressWarnings("unchecked")
@Override
protected String doInBackground(String... arg0) {
try {
urlAudio = new URL("http://server/Mora/");
} catch (MalformedURLException e) {
e.printStackTrace();
}
ApacheURLLister lister1 = new ApacheURLLister();
try {
myList = lister1.listAll(urlAudio);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
if (pDialog.isShowing()) {
pDialog.dismiss();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
MainActivity.this, android.R.layout.simple_list_item_1,
myList);
adapter.notifyDataSetChanged();
mListView.setAdapter(adapter);
mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
mListView.setCacheColorHint(Color.TRANSPARENT);
}
}
class PlaySongAsy extends AsyncTask<String, Void, Boolean> {
String baseURL;
public PlaySongAsy(String baseURL) {
this.baseURL = baseURL;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
@Override
protected Boolean doInBackground(String... urls) {
new Thread() {
@Override
public void run() {
playSong(baseURL);
}
}.start();
return true;
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
// progressDialog.dismiss();
}
}
public void onCompletion(MediaPlayer mp) {
}
问题是你正在使用标准的ArrayAdapter
和布局,它将把每个URL转换成字符串并显示在列表中,因此你在列表中有完整的URL。
相反,创建了自己的适配器并使用URL.getFile()
,它只返回像URL.getFile()
这样的文件名,您可以将整个url保存在setTag()
的布局视图中,因此在单击方法上,您仍然可以检索整个url并执行其余的处理。
试试这个教程,它用简单的方式解释了ArrayAdapter
的用法: