我正在使用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) {
}
答案 0 :(得分:0)
问题是您使用的是标准ArrayAdapter
和布局,它只是将每个URL转换为字符串并显示在列表中,因此您在列表中有完整的URL。
而是创建了自己的适配器并使用URL.getFile()
,它只返回URL.getFile()
这样的文件名,您可以使用setTag()
将整个网址保存在布局视图中,以便点击方法您仍然可以检索整个网址并完成剩余的处理。
尝试本教程,以简单的方式解释ArrayAdapter
用法:
https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView