Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do transverse data in Handler thread

I have an activity, which contains 2 fragments, each fragment has 2 list views, each has an adapter. So I get pretty long data in json from the server, I use AsyncTask and on post execute, I parse the data. The problem is that it freezes the loading animation for 4-5 seconds.

I have tried to use Handler thread, but the problem is still there, I must be doing something terribly wrong here.

public class DataService extends AsyncTask<String, String, Void> {

    @Override
        protected Void doInBackground(String... params) {

        url = new URL(DATA_URL);    
        //Connection stuff

        String jsonString = stringBuilder.toString();
        jsonObject = new JSONObject(jsonString);

        //Adding data in Java Objects

    }


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


             //Upcoming List in Fragment
             allDataFragment.setUpcomingDataList(UpcomingAllDataList);
             awayFragment.setUpcomingDataList(tUpcomingAwayList);
             homeFragment.setUpcomingDataList(tUpcomingHomeList);


             //Past List in Fragment            
             allDataFragment.setPastDataList(pastAllDataList);
             awayFragment.setPastDataList(pastAwayList);
             homeFragment.setPastDataList(pastHomeList);    

    }

I added log messages in adapter, and I can see that at the time of parsing rows it freezes, so it take all the post execute stuff in handler thread

       Handler handler= new Handler();

        handler.post(new Runnable() {
            @Override
            public void run() {


                    //Upcoming List in Fragment
                     allDataFragment.setUpcomingDataList(UpcomingAllDataList);
                     awayFragment.setUpcomingDataList(tUpcomingAwayList);
                     homeFragment.setUpcomingDataList(tUpcomingHomeList);


                     //Past List in Fragment            
                     allDataFragment.setPastDataList(pastAllDataList);
                     awayFragment.setPastDataList(pastAwayList);
                     homeFragment.setPastDataList(pastHomeList);    


            }                   
            });

I also tried to add a handler in Adapter but it fails to load UI component from there.

This is a snippet from my fragment code.

if (pastListView != null) {
        PastListAdapter allGamesAdapter = new PastListAdapter(getContext(), pastAllDataList);
        pastListView.setAdapter(allGamesAdapter);
        }

and in Adapter i am doing following.

  @Override
    public View getView(int position, View convertView, ViewGroup parent) {

            TextView vScore = (TextView) v.findViewById(R.id.v_score);
            TextView date = (TextView) v.findViewById(R.id.date);
            ImageView image = (ImageView) v.findViewById(R.id.iv_image);
            //7 Other views


            vScore.setText(dataList.get(position).getScore);
            date.setText(dataList.get(position).date);

             Log.d(TAG, "getView: Row");

            return v;
    }

The loading animation works fine in start but at the time it starts parsing the data in adapter, it hangs t he UI thread.

like image 840
dev90 Avatar asked Mar 07 '23 20:03

dev90


2 Answers

Use AsyncTask to handle work items shorter than 5ms ( 5 milli seconds) in duration. Since you are getting pretty long JSON data from server, you have to look at other alternatives ( Thread, HandlerThread and ThreadPoolExecutor)

Even though you create Handler, it's running on UI Thread only. Instead of Handler, use HandlerThread

Proposed solution:

  1. Use HandlerThread to get JSON data from server. UI Thread is not blocked to get the data.

  2. Post Data from HandlerThread to Handler of UI Thread. UI Handler will take care of updating UI with data received on handleMessage(Message msg)

Useful posts:

Asynctask vs Thread vs Services vs Loader

Handler vs AsyncTask vs Thread

Why to use Handlers while runOnUiThread does the same? ( This post contains code example for above proposed solution)

like image 105
Ravindra babu Avatar answered Mar 10 '23 11:03

Ravindra babu


Use Volley instead of AsyncTask

like image 44
Shahzad Akhtar Avatar answered Mar 10 '23 09:03

Shahzad Akhtar