-1

Calling the method:

Log.i("MY_TAG 0 =", String.valueOf(findViewById(R.id.listView)));
allUsers(host, json, login, this);

My method:

private static void allUsers(String host, String json, String login, Activity activity) {
    ListView lv1 = activity.findViewById(R.id.listView);

    Log.i("MY_TAG 1 =", String.valueOf(activity));
    Log.i("MY_TAG 2 =", String.valueOf(lv1));
    Log.i("MY_TAG 3 =", String.valueOf(activity.findViewById(R.id.listView)));

    new AsyncTask<Void, String, String>() {
        @Override
        protected String doInBackground(Void... params) {
            return Http.sendMsg(host, json);
        }
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            AllUsersJson allUsersJson = new Gson().fromJson(s, AllUsersJson.class);
            GetAllUsersAdapter myAdapter = new GetAllUsersAdapter(allUsersJson, activity.getApplicationContext(), login);
            ListView lv2 = activity.findViewById(R.id.listView);
            Log.i("MY_TAG 4 =", String.valueOf(lv1));
            Log.i("MY_TAG 5 =", String.valueOf(lv2));
            lv2.setAdapter(myAdapter);
        }
    }.execute();
}

Logs:

MY_TAG 0 =: android.widget.ListView{1255189 VFED.VC.. .F....ID 0,0-1080,1584 #7f080071 app:id/listView}
MY_TAG 1 =: myzabbix.sadrutdin.zaynukov.com.testzabbix.NavDrawerActivity@231f1a
MY_TAG 2 =: android.widget.ListView{1255189 VFED.VC.. .F....ID 0,0-1080,1584 #7f080071 app:id/listView}
MY_TAG 3 =: android.widget.ListView{1255189 VFED.VC.. .F....ID 0,0-1080,1584 #7f080071 app:id/listView}
MY_TAG 4 =: android.widget.ListView{1255189 VFED.VC.. ......ID 0,0-1080,1584 #7f080071 app:id/listView}
MY_TAG 5 =: null

Replace lv2.setAdapter(myAdapter); by lv1.setAdapter(myAdapter);

Result:

MY_TAG 0 =: null
MY_TAG 1 =: myzabbix.sadrutdin.zaynukov.com.testzabbix.NavDrawerActivity@ce89be3
MY_TAG 2 =: null
MY_TAG 3 =: null
MY_TAG 4 =: null
MY_TAG 5 =: null

The strangest thing is that an identical method works without problems. I tried all the options, but apparently I do not understand something in Android ...

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

1

Remove all activity references from that method.
Rewrite using a Callback

public interface OnUsersJson() {
    void onUsers(AllUsersJson users);
}

Refactor the AsyncTask method to accept that interface, knowing nothing about the Activity that calls it

public static void allUsers(String host, String json, String login, final OnUsersJson listener) {
    new AsyncTask<Void, String, String>() {
        @Override
        protected String doInBackground(Void... params) {
            return Http.sendMsg(host, json);
        }
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            AllUsersJson allUsersJson = new Gson().fromJson(s, AllUsersJson.class);
            if (listener!=null) listener.onUsers(allUsersJson);
        }
    }.execute();
}

Move all view usage back over to the Activity and call your API class, passing in the Callback to populate the list of that Activity

In onCreate

this.lv1 = findViewById(R.id.listView);
 // Passing an Arraylist to this constructor should be optional 
this.myAdapter = new GetAllUsersAdapter(MainActivity.this, login);
lv1.setAdapter(myAdapter);
API.allUsers(host, json, login, new OnUsersJson() {
    @Override public void onUsers(AllUsersJson users) {
         myAdapter.add(); // Add users here 
         // Do not assign users to a field of this Activity 
     } 
});
// if you did assign a field of users, or added to an arraylist, it'll be null/empty here 

By the way, "all users" is a poor name for a JSON object. You should use Gson to create a List<User> instead

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245