-1

In my application i should used socket.io and i want when receive my event update UI elements!
I write below codes and i receive my events show me logs, but not update any UI!

I want when receive event, check this winner is user or not and then update my UI.
In logCat show me my logs but not update any UI elements!

My codes:

public void onsocketFinishRecieve(final JSONObject ob) {
        try {
            ((BaseActivity) context).runOnUiThread(() -> {
                try {
                    cancelTimer();
                    final FinishResponse finishResponse = new Gson().fromJson(ob.toString(), FinishResponse.class);
                    if (finishResponse.getRes().getWinnerName().equals("not user") || finishResponse.getRes().getWinnerName().equals("not winner")) {
                        winnerNameWhenFinished = "Not winner";
                    } else {
                        winnerNameWhenFinished = finishResponse.getRes().getWinnerName();
                    }
                    if (detail.getId() != null) {
                        if (detail.getId() == finishResponse.getRes().getId()) {
                            //Set new winner layouts
                            //Register in auction
                                if (Constants.profileResponse != null) {
                                    if (Constants.profileResponse.getRes() != null) {
                                        if (Constants.profileResponse.getRes().getUser() != null) {
                                            //Winner
                                            if (Constants.profileResponse.getRes().getUser().getId().equals(finishResponse.getRes().getUserId())) {
                                                Log.e("FinishedSocket", "1");
                                                detailPage_bottomWinnerRateTxt.setVisibility(View.GONE);
                                                detailPage_bottomWinnerBuyTxt.setText("Show basket");
                                                detailPage_bottomWinnerBuyTxt.setOnClickListener(v -> {
                                                    Intent intent = new Intent(context, MainActivity.class);
                                                    intent.putExtra("OPEN_CART_IN_MAIN", "true");
                                                    startActivity(intent);
                                                });
                                            } else {
                                                Log.e("FinishedSocket", "2");
                                                //Loser
                                                detailPage_bottomWinnerBuyTxt.setText("Awesome offers");
                                            }
                                        }
                                    }
                                }
                        }
                    }
                } catch (Exception e) {
                    Log.e("DetailResErr", e.getMessage());
                }
            });
        } catch (Exception e) {
            Log.e("DetailResErr", e.getMessage());
        }
    }

Logcat message :

2020-03-08 13:37:37.399 E/FinishedSocket: 2

In logcat show me above message , why not run this line : detailPage_bottomWinnerBuyTxt.setText("Awesome offers"); ??

How can i fix it?

Dr.KeyOk
  • 1
  • 4

2 Answers2

0

try to run this on Mainthread like this.

someActivity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
           //Your code to run in GUI thread here
           detailPage_bottomWinnerBuyTxt.setText("Awesome offers");
        }
});
Pratik Patel
  • 57
  • 2
  • 12
  • Thanks my friend. I write below codes but again not work me and not update UI ! `((AuctionDetailPage) context).runOnUiThread(new Runnable() { @Override public void run() { //Your code to run in GUI thread here detailPage_bottomWinnerBuyTxt.setText("Awesome offers"); } });` – Dr.KeyOk Mar 08 '20 at 10:59
0

Sockets work on IOThread while other side UI Work on separate Thread called UI thread. So, update the UI element on UI Thread. Use Annotation :

@UiThread
public abstract void setText(@NonNull String text) { ... }

For know about more annotation, Check the following blog: https://medium.com/@gurwindersingh_37022/android-annotations-30b4a2850d0

  • Thanks my friend, but can you send to me code with my above codes? please my friend, i realle need your help .pelase – Dr.KeyOk Mar 08 '20 at 12:16