Reputation: 5234
I have following code to get data from server to list-view.
public class ServerFileList extends Activity {
URL urlAudio;
ListView mListView;
ProgressDialog pDialog;
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();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(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 getAudiofromServer extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ServerFileList.this);
pDialog.setMessage("Uploading audio file to server. Please wait ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
@SuppressWarnings("unchecked")
@Override
protected String doInBackground(String... arg0) {
try {
System.out.println("Inside first try");
urlAudio = new URL("server-name/folder/folder");
} catch (MalformedURLException e) {
System.out.println("Inside first catch");
e.printStackTrace();
}
ApacheURLLister lister1 = new ApacheURLLister();
try {
System.out.println("Inside second try");
myList = lister1.listAll(urlAudio);
} catch (IOException e) {
System.out.println("Inside second catch");
e.printStackTrace();
}
System.out.println("ABCD===" + myList);
return null;
}
protected void onPostExecute(String file_url) {
if (pDialog.isShowing()) {
pDialog.dismiss();
}
}
}
}
So basically what i am trying is listing all audio files from server to list-view, so i got code from this link and i have bit modified as per my requirement. But still list-view is empty, it is not showing any data.
I have also imported ivy-2.0.0-rc1.jar inside my project..
So what i am doing wrong. How can i list all file of server's particular folder inside list-view. Is this correct or do i need to use some other approach.
Thank you.
Upvotes: 0
Views: 172
Reputation: 13520
Move the following code
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, myList);
adapter.notifyDataSetChanged();
mListView.setAdapter(adapter);
mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
mListView.setCacheColorHint(Color.TRANSPARENT);
to onPostExecute
of getAudiofromServer
. If you do this on onCreate
its possible that getAudiofromServer
may not have completed execution and hence myList
may be populated. So the ListView
will not show any items in it.
If you do it in onPostExecute
you will have got the list of audio files stored on your server in myList
and so they will be seen in the ListView
Upvotes: 2
Reputation: 133560
Your myList
is not populated until doInbackground
does its computation.
Move this
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);
to onPostExecute
. Make sure myList
is populated.
Upvotes: 1