2

i used an example from this question, but when i try to run the app i get a blank listview (white screen).

i already tried to create the layout because it was not present but it doesn't work.

<?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>

how to solve it?

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);
    }
}
}
Orbi
  • 61
  • 8
  • 2
    Youll have to create a ListViewAdapter and pass it the array of all mo3 items you have. If you only will have just a Listview it will not render anything as it does not have any items to show. – Ahmed Feb 22 '16 at 22:23
  • i know but in the example it is already giving an Adapter, so how to do that? – Orbi Feb 22 '16 at 23:10
  • @Ahmed i updated the question – Orbi Feb 22 '16 at 23:43

1 Answers1

1

You need to pass data into an Adapter that will inflate a view per list item, bind the data to it and display it. I'd suggest taking a look at this article

Ian
  • 2,024
  • 2
  • 12
  • 12