Koc
Koc

Reputation: 232

Android Threads, Fragments and Adapters

I have created a custom adapter that inflates rows in a fragment. I would like to know how to put this in a thread.

In my fragment I have:

context = getActivity().getApplicationContext();
ListAdapter adapter = new NotesAdapter(courseId, context);
setListAdapter(adapter);

Every thing works this way but I have tried to put this in all four ways (AsyncTask, Java thread...) that Android offers for multithreading but the adapter won`t start that way. It just shows blank screen.

Can anyone help me how to put this in a separate thread?

Upvotes: 0

Views: 497

Answers (1)

Aerrow
Aerrow

Reputation: 12134

For your reference,

public class SampleTask extends AsyncTask<Void, Void, Void> {


        @Override
        protected Void doInBackground(Void... params) {
            // Do your Background process Eg.. some Parsing whatever it's, then paste you adapter initialization code
            // Initialize your adapter as global
        CustomAdapter sampleAdapter = new CustomAdapter(CurrentActivity.this,
                        R.id.ImageView01, <Your Arraylist/Array>);
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);

            // Here you set your adapter
            listView.setAdapter(sampleAdapter);

        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
        }
    }

Upvotes: 1

Related Questions