0

I am making Scoreboard app and It is working well but small issue is the Scoreboard is blinking

Just for example:

Shikhar Dhawan 86 (126)

Rohit Sharma 20(20)

the whole list is blinking in ms(millisecond)

here is a short code:

public class handleBetTask extends AsyncTask<JSONObject, Void, Void> {
    @Override
    protected Void doInBackground(JSONObject... voids) {
        try {
            JSONObject response = voids[0];
            JSONArray dataArray = response.getJSONArray("Data");
            if (dataArray.length() > 0) {
                        groupWiseScoreGenerate(dataArray);
                    } else {
                        matchedScoreGroup.clear();//Here matchedScoreGroup is ArrayList
            }
    }
}

Now groupWiseScoreGenerate(dataArray):

private void groupWiseScoreGenerate(JSONArray array) {
    matchedScoreGroup.clear();
    //Here is a data insertion in matchedScoreGroup
     runOnUiThread(new Runnable() {
                @Override
                public void run() {

        MatchedScoreFragment.getInstance().setMatchedScoreGroup(matchedScoreGroup);//where it is bind to recyclerview
    }

In MatchedScoreFragment (fragment) where score is set.

public void setMatchedScoreGroup(ArrayList<Match> matchedScoreGroup) {
    try {

        if (matchedScoreGroup.size() > 0) {
            if (txtEmptyView.isShown()) {
                txtEmptyView.setVisibility(View.GONE);
            }
            mRecyclerView.setVisibility(View.VISIBLE);
            this.matchedScoreGroup= matchedScoreGroup;
            adapter.notifyDataChanged(this.matchedScoreGroup);
        } else {
            mRecyclerView.setVisibility(View.GONE);
            txtEmptyView.setVisibility(View.VISIBLE);
        }
    }catch (Exception e){
        e.printStackTrace();
    }
}

it is working well but it is called for every 200 ms so blinking is happening when showing score i.e txtEmptyView.setVisibility(View.VISIBLE); called and scoreboard gone for few ms.I had just shown limited code but you will get easily what is done

problem may be because of runOnUiThread. Appreciate for help thank you.

  • You can avoid to use the runOnUiThread() there because it's the last action, so you can move the entire "groupWiseScoreGenerate()" content in an "onPostExecute()" and remove the runOnUiThread(). However I'm not sure it depends of it. It seems that your "setMatchedScoreGroup(ArrayList)" is called only with "ArrayList.size() > 0", so the "mRecyclerView.setVisibility(View.GONE);" is never executed. I think the problem is somewhere else. Is you "MatchedScoreFragment.getInstance().setMatchedScoreGroup()" executed only from there? – emandt Jul 24 '18 at 05:59
  • yeah you are ryt "ArrayList.size() > 0" but thing is i had checked with log in runonuithread it is completely ok but when it goes to setMatchedScoreGroup(arraylist) sometimes arraylist is coming 0 thats why it is blinking.Is there any other method? – Abhishek Bardolia Jul 24 '18 at 06:04
  • I wrote you: is your "setMatchedScoreGroup()" called from somewhere else? Because from the piece of code you put here that method cannot be executed when the argument is Zero. So there should be another place that calls that method with an empty ArrayList... – emandt Jul 24 '18 at 06:09
  • Yeah it is called from 3 side and I had tested same like setMatchedScoreGroup(ArrayList matchedScoreGroup,int i) here i is where it came from...mainly i am getting from runonuithread side. – Abhishek Bardolia Jul 24 '18 at 06:11
  • Sorry I am stupid: you are never using the "array" parameter in your "groupWiseScoreGenerate()" method. You have to call "MatchedScoreFragment.getInstance().setMatchedScoreGroup(array);"!! Or fill your "matchedScoreGroup" variable with that "array" data. – emandt Jul 24 '18 at 06:12
  • then whole code should write in that MatchedScoreFragment alright?? – Abhishek Bardolia Jul 24 '18 at 06:13
  • Your new data is in "JSONArray array" but you never use it. You're clearing "matchedScoreGroup" variable but not filled it with any new data. You have to fill it! – emandt Jul 24 '18 at 06:16
  • sir just go one more time to comment in groupWiseScoreGenerate()... //Here is a data insertion in matchedScoreGroup here there is insertion which i had not shown code. – Abhishek Bardolia Jul 24 '18 at 06:17
  • my mistake that i had not mentioned clearly but there is a big code and just shown comment. – Abhishek Bardolia Jul 24 '18 at 06:19
  • So the problem is there (the piece you not shown) or that the JSON return an empty "data" sometimes. About my second point: you have to check why it retuen an empty data. – emandt Jul 24 '18 at 06:21
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/176612/discussion-between-abhishek-bardolia-and-emandt). – Abhishek Bardolia Jul 24 '18 at 06:23

1 Answers1

1

The problem is that the doInBackground() is called too quick and clears the "matchedScoreGroup" array before the UiThread could process the Runnable. So the solution is: (1) declare "ArrayList<> doInBackground()" and return here your converted/filled ArrayList (2) create the "AsyncTask.onPostExecute(ArrayList<>)" method and run your "MatchedScoreFragment.getInstance().setMatchedScoreGroup()" from there

emandt
  • 2,547
  • 2
  • 16
  • 20
  • it doesnt work smoothly when code is pasted to onPostExecute.solution is done but not working smoothly. – Abhishek Bardolia Jul 24 '18 at 11:29
  • Because the update in the graphic interface is done 200ms (the timer you said to have set) + the time the IDE takes to run the command "onPostExecute()" method. Using your previous way you had only 200ms. The only solution is to create an Handler in the Activity (or where you have the graphic interface) and in the previous "onPostExecute()" send a "Handler.sendMessage()" to that Handler. In this way your "onPostExecute()" doesn't do any job but it is all demanded to the UiThread in a later pass. – emandt Jul 24 '18 at 11:34
  • I'm on a mobile version of this website and I cannot format the Code, however: (1) in the MainActivity (or where you have the graphic interface) you create an Handler: "Handler mHandler = new Handler(Looper.getMainLooper()){@Override public void handleMessage(Message message){if (message.what == 123) MatchedScoreFragment.getInstance().setMatchedScoreGroup((ArrayList)message.obj);}};" (2) then in the previous "onPostExecute()" just send a "signal" to it using "....mHandler.sendMessage(Message.obtain(...mHandler, 123, 0, 0, yourArrayList));" – emandt Jul 24 '18 at 11:43
  • I think you should follow some Guide about Thread Communication, however you can following this my piece of code (different example but same solution): https://stackoverflow.com/questions/51458200/syncing-progressbar-with-handler/51458427#51458427 – emandt Jul 24 '18 at 12:11
  • yeah i have to see thread communication but right now just tell where do i write MatchedScoreFragment.getInstance().setMatchedScoreGroup(matchedScoreGroup); in groupwiseScoreGenerate or else? – Abhishek Bardolia Jul 24 '18 at 12:16
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/176644/discussion-between-emandt-and-abhishek-bardolia). – emandt Jul 24 '18 at 12:19