0

I want to access and populate a ListView from seprate thread... But its object has not its scope in new thread... What is the solution for it?

ListView FilesListView;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    FilesListView= (ListView)findViewById(R.id.remoteFilesListView);



new Thread ( new Runnable() {

        @Override
        public void run() {

    // fetch data from server…
    String xmlFormServer = Worker.getXmlresponse();
    Log.d("Response from Serever", xmlFormServer;


    // FilesListView object of Listview is not accessable in this thread to populate data… 
        }
    }).start();

}
CodingLover
  • 47
  • 2
  • 11
  • 1
    Ideally use AsyncTask for thread operations that you want to do in Android. ITs optimized for it and provides UI Thread methods to update your UI. Plus this way you are trying to update UI from a background thread which will cause your app to crash. – Kapil G Aug 15 '17 at 12:14

2 Answers2

0

You should to implement adapter for your ListView

Try to follow pattern below

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    FilesListView = (ListView) findViewById(R.id.remoteFilesListView);

    adapter = new SimpleAdapter(/*...*/); // implement your custom adapter or use native
 //        1. set adapter into FileListView
    FilesListView.setAdapter(adapter);

    new AsyncTask<Void, Void, List>() {

        @Override
        protected List doInBackground(Void... params) {
            String xmlFormServer = Worker.getXmlresponse();
            Log.d("Response from Serever", xmlFormServer;

 //                2. READ YOUR DATA HERE

            List result = null; //

 //                3. send result to ui thread
            return result;
        }

        @Override
        protected void onPostExecute(List list) {
 //                the method executes on ui thread
 //                4. put your data into adapter
 //                you should implement the method or create native adapter
            adapter.setDate(list);
 //                5. refresh list - only UI THREAD can do this
            adapter.notifyDataSetChanged();
            super.onPostExecute(list);
        }
    }.execute();
}
Alex Nuts
  • 977
  • 8
  • 11
0

You cannot populate populate ListView/RecyclerView or any other view or component on another thread.

That has to be on the MainThread(UIThread).

Checkout this link for more info about UIThread in android and how its works.

What is the Android UiThread (UI thread)

However, you can put the logic or IO operation on different thread asynchronously and then you can put populate the result on UI thread.

One approach would be to use AsyncTask.

https://developer.android.com/reference/android/os/AsyncTask.html

Something like this..

new AsyncTask<Void, Void, String >() {

    // Runs on Worker thread
    @Override
    protected String doInBackground(Void... params) {
        String xmlFormServer = Worker.getXmlresponse();
        return xmlFormServer;
    }

    // Runs on Ui thread
    @Override
    protected void onPostExecute(String data) {
        if(data != null){
            adapter.setDate(data);
            adapter.notifyDataSetChanged();
        }

    }
}.execute();
Ritt
  • 3,181
  • 3
  • 22
  • 51
  • you don't need to create a new thread, asynctask creates a new thread for you. do all the time taking stuff in doInBackground() this happens on worker thread. please go thru the link provided of asynctask. – Ritt Aug 15 '17 at 13:46