I just can’t understand what's the matter: When I load a listView into onCreate, the listView doesn’t show anything, as soon as you turn the screen off and on, the list is displayed. Help me understand the reason I'm in a stupor. AsyncTask code:

// RemoteDataTask AsyncTask private class RemoteDataTask extends AsyncTask<Void, Void, Void> { @Override protected void onPreExecute() { super.onPreExecute(); backendlessInitialize(); mProgressDialog = new ProgressDialog(ShowPUSH.this); mProgressDialog.setTitle(text1); mProgressDialog.setMessage(text2); mProgressDialog.setIndeterminate(false); mProgressDialog.show(); Typeface avenirnextregular= Typeface.createFromAsset(getAssets(), "avenirnextregular.ttf"); ((TextView) mProgressDialog.findViewById(getResources().getIdentifier("alertTitle", "id", "android"))).setTypeface(avenirnextregular); } @Override protected Void doInBackground(Void... params) { worldpopulationlist = new ArrayList<notifications>(); try { Backendless.Persistence.of( notifications.class).find(new AsyncCallback<BackendlessCollection<notifications>>(){ @Override public void handleResponse(BackendlessCollection<notifications> notification) { for (notifications pushes: notification.getData()){ notifications push = new notifications(); push.setPush((String) pushes.getPush()); push.setDatePush((String) pushes.getDatePush()); worldpopulationlist.add(push); } } @Override public void handleFault(BackendlessFault backendlessFault) { Log.i ("listView33", "error: " + backendlessFault.getMessage()); }}); } catch (Exception e) { Log.e("Error", e.getMessage()); e.printStackTrace(); } return null; } @Override protected void onPostExecute(Void result) { listview = (ListView) findViewById(R.id.pushes_listview); adapter = new PUSHListViewAdapter(ShowPUSH.this, worldpopulationlist); listview.setAdapter(adapter); mProgressDialog.dismiss(); } } 

Full activation code:

 public class ShowPUSH extends Activity implements SwipeRefreshLayout.OnRefreshListener { // Declare Variables ListView listview; List<ParseObject> ob; ProgressDialog mProgressDialog; PUSHListViewAdapter adapter; private List<notifications> worldpopulationlist; private SwipeRefreshLayout mSwipeRefreshLayout; String text1; String text2; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.push_listview_main); setBack(); // setIcons(); SharedPreferences preferences_blurSwitch = getSharedPreferences("TEMP_BLUR", Context.MODE_PRIVATE); Integer blurSwitch = preferences_blurSwitch.getInt("TEMP_BLUR", 0); if (blurSwitch == 1){ } else{ blur(); } swipe(); // Execute RemoteDataTask AsyncTask new RemoteDataTask().execute(); } public void swipe(){ mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container); mSwipeRefreshLayout.setOnRefreshListener(this); mSwipeRefreshLayout.setColorSchemeColors(Color.RED, Color.GREEN, Color.BLUE, Color.CYAN); } @Override public void onRefresh() { new RemoteDataTask().execute(); new Handler().postDelayed(new Runnable() { @Override public void run() { mSwipeRefreshLayout.setRefreshing(false); } }, 4000); } public void backendless(){ SharedPreferences YOUR_APP_ID_sp = getSharedPreferences("BACKENDLESS_APP_ID", Context.MODE_PRIVATE); SharedPreferences YOUR_SECRET_KEY_sp = getSharedPreferences("BACKENDLESS_SECRET_KEY", Context.MODE_PRIVATE); SharedPreferences APP_VERSION_sp = getSharedPreferences("BACKENDLESS_APP_VERSION", Context.MODE_PRIVATE); String YOUR_APP_ID = YOUR_APP_ID_sp.getString("BACKENDLESS_APP_ID", ""); String YOUR_SECRET_KEY = YOUR_SECRET_KEY_sp.getString("BACKENDLESS_SECRET_KEY", ""); String APP_VERSION = APP_VERSION_sp.getString("BACKENDLESS_APP_VERSION", ""); Backendless.initApp( this, YOUR_APP_ID, YOUR_SECRET_KEY, APP_VERSION ); } 

Adapter Code:

 public class PUSHListViewAdapter extends BaseAdapter { Typeface avenirnextbold; Typeface avenirnextregular; // Declare Variables Context context; LayoutInflater inflater; private List<notifications> worldpopulationlist = null; private ArrayList<notifications> arraylist; public PUSHListViewAdapter(Context context, List<notifications> worldpopulationlist) { this.context = context; this.worldpopulationlist = worldpopulationlist; inflater = LayoutInflater.from(context); this.arraylist = new ArrayList<notifications>(); this.arraylist.addAll(worldpopulationlist); } } public class ViewHolder { TextView rank; TextView country; TextView population; ImageView flag; } @Override public int getCount() { android.util.Log.i ("listView33", "worldpopulationlist.size(): " + worldpopulationlist.size()); return worldpopulationlist.size(); } @Override public Object getItem(int position) { return worldpopulationlist.get(position); } @Override public long getItemId(int position) { return position; } public View getView(final int position, View view, ViewGroup parent) { final ViewHolder holder; if (view == null) { holder = new ViewHolder(); view = inflater.inflate(R.layout.push_listview_item, null); // Locate the TextViews in listview_item.xml holder.rank = (TextView) view.findViewById(R.id.push_message); holder.country = (TextView) view.findViewById(R.id.date_push); } else { holder = (ViewHolder) view.getTag(); } // Set the results into TextViews holder.rank.setText(worldpopulationlist.get(position).getPush()); holder.country.setText(worldpopulationlist.get(position).getDatePush()); android.util.Log.i ("listView33", "older.rank.setText(worldpopulationlist.get(position).getPush());: " + worldpopulationlist.get(position).getPush()); } holder.country.setTypeface(avenirnextbold); holder.rank.setTypeface(avenirnextregular); } catch(Exception nullEx){ } return view; } 

}

    2 answers 2

    I think the point is that you run another asynchronous task in the doInBackground and, as a result, onPostExecute is executed before its completion. As a result, your list is initially empty.

    In any case, filling the list with data inside the doInBackground itself is a bad idea. You need to form it there, transfer it to onPostExecute and it is there that you add data to the activation list and then transfer the list to the adapter. The list is not the one that was received in onPostExecute , but the one that lies in the onPostExecute .

    It is necessary to add data to the list using the addAll method, and not by redirecting the list object to a link.


    In the activity, initially initialize the list with the data.

     private List<notifications> worldpopulationlist = new ArrayList<>(); 

    The download task should look like this:

     private class RemoteDataTask extends AsyncTask<Void, Void, ArrayList<notifications>>{ @Override protected ArrayList<notifications> doInBackground(Void... params) { ArrayList<notifications> worldpopulationlist = new ArrayList<notifications>(); //наполняем данными и возвращаем их return worldpopulationlist; } @Override protected void onPostExecute(ArrayList<notifications> result) { listview = (ListView) findViewById(R.id.pushes_listview); worldpopulationlist.clear(); worldpopulationlist.addAll(result); adapter = new PUSHListViewAdapter(ShowPUSH.this, worldpopulationlist); listview.setAdapter(adapter); } } 
    • @ user207840, some code added. - Yuriy SPb
    • one
      You were right: I changed Backendless.Persistence.of (Contact.class) .findLast (new AsyncCallback <Contact> () {..... to Contact lastContact = Backendless.Persistence.of (Contact.class) .findLast () ; (that is, I just redid the synchronous request), and everything began to work. Thank you! - user207840
    • one
      Thanks also for the example! - user207840

    Try this

     listView.post(new Runnable() { @Override public void run() { listview.setAdapter(adapter); } }); 
    • In onPostExecute? I tried: unfortunately, it does not help - the same effect. Perhaps you have other suggestions? - user207840
    • Do you call something in onResume ()? give the activation code - Yury Pashkov
    • In onResume () I cause nothing. He is not even in this class. I updated the whole Activit code, removing precisely the non-essential voids. Perhaps the Adapter code can help (I added it to the question)? - user207840
    • I noticed that here: stackoverflow.com/questions/36135766/… a synonymous situation, however, and the proposed option does not fit. - user207840 6:38 pm