如何从服务器查看mp3文件的列表视图

时间:2016-02-22 22:21:39

标签: android listview android-arrayadapter

我使用了来自this question的示例,但是当我尝试运行该应用时,我得到一个空白的列表视图(白色屏幕)。

我已经尝试创建布局,因为它不存在但是它不起作用。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<ListView
    android:id="@+id/listAudio"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:divider="#242424"
    android:dividerHeight="1dp"
    android:listSelector="@drawable/list_selector" />
</LinearLayout>

如何解决?

public class ServerFileList extends Activity {

URL urlAudio;
ListView mListView;
ProgressDialog pDialog;
private MediaPlayer mp = new MediaPlayer();
private List<String> myList = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.serverfilelist);

    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) {
            playSong(urlAudio + myList.get(position));
        }
    });
}

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://i-qualtech.com/Fidol/uploadAudio");
            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(ServerFileList.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://i-qualtech.com/Fidol/uploadAudio");
        } 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>(
                ServerFileList.this, android.R.layout.simple_list_item_1,
                myList);
        adapter.notifyDataSetChanged();
        mListView.setAdapter(adapter);
        mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        mListView.setCacheColorHint(Color.TRANSPARENT);
    }
}
}

1 个答案:

答案 0 :(得分:1)

您需要将数据传递到适配器,该适配器将为每个列表项扩展视图,将数据绑定到它并显示它。 我建议看看this article