0

I have a service that removes an object from my webservice and return the current List. Each time I request the service, it updates my textview (with the size of the list) and my adapter. The problem is when I go to the MainActivity and return to the activity which do this service, the textview and the adapter did not update anymore.

apiServices.deleteEmployee(new APIServices.BookmarkCallback() {
   @Override
   public void onSuccess(final ArrayList<Employee> list) {  

  remainingPeople.setText(getResources().getString(R.string.remaining_people) + list.size());
  adapter = new ArrayAdapter<>(getApplicationContext(), 
            android.R.layout.simple_list_item_1, list);
  employeesList.setAdapter(adapter);


}

The code is setted on my onCreate() method. The idea is still update the textview when return to the activity.

onCreate() method:


 @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);


        // Remove notification bar
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        //set content view AFTER ABOVE sequence (to avoid crash)
        this.setContentView(R.layout.activity_count);

        remainingPeople = findViewById(R.id.peopleCounter);


        findBT();
        openBT();


        @SuppressWarnings("unchecked")
        ArrayList<Employee> list = (ArrayList<Employee>) getIntent()
                .getSerializableExtra(SEND_EMPLOYEES_LIST);


        remainingPeople.setText(getResources().getString(R.string.remaining_people) + list.size());

        employeesList = findViewById(R.id.employees_list);
        adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list);
        employeesList.setAdapter(adapter);


        beep = new ToneGenerator(AudioManager.STREAM_ALARM, 900);

        handler = new Handler(Looper.getMainLooper()) {
            @Override
            public void handleMessage(Message message) {
                switch (message.what) {
                    case READ_ARRAY_BYTE:
                        byte[] arrayBytes = new byte[message.arg1];
                        System.arraycopy(message.obj, 0, arrayBytes, 0, message.arg1);
                        try {
                            String epc = new String(arrayBytes, "UTF-8");
                            beep.stopTone();
                            epc = epc.substring(0, 24);
                            apiServices.deleteEmployee(new APIServices.BookmarkCallback() {
                                @Override
                                public void onSuccess(final ArrayList<Employee> list) {
                               //this part updates after the API response
                                    remainingPeople.setText(getResources().getString(R.string.remaining_people) + list.size());
                                    adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, list);
                                    employeesList.setAdapter(adapter);

                                }

                                @Override
                                public void onError() {

                                }
                            }, epc);
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                        break;


//...

2 Answers2

0

Try to put the process that update your textview in onResume() instead of onCreate()

@Override
 public void onResume(){ 
 super.onResume(); 
 // put your code here...
 }
ismail alaoui
  • 5,748
  • 2
  • 21
  • 38
  • Thanks, @ismail alaoui, but this not solve my problem because I have a handler which wait for a response with the object that I send to the API remove. After this my textview/adapter will be updated. The problem is when I go for the second time to the activity which waits the API answer and which update my textview/adapter. It sends to the API and remove, but not update my UI. – João Vitor Brasil Jun 04 '19 at 12:14
  • you have to put setText inside your handler and our handler inside your onResume as i suggest you , try it – ismail alaoui Jun 04 '19 at 15:04
0

I found a solution through michiakig's answer where I can declarate my textview as static, but I can have problems with that. So, another way is save using Activity.onSaveInstanceState. Thank you for all who answer me.