After returning a fragment from the previous screen, the adapter update does not work, and the toast in onPostExecute() works fine.

What could be the reason?

I noticed such a thing: if you completely reassign the adapter in onCreateView to else , everything works, although it becomes empty, but everything works.

 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); activity = getActivity(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); View rootView = inflater.inflate(R.layout.screen_two, container, false); mImageLoader = ImageLoader.getInstance(); mImageLoader.init(new UILsettings().getConf(this.getActivity())); // footer footer = inflater.inflate(R.layout.footer, null); btnLoadMore = (Button) footer.findViewById(R.id.loadMore); btnLoadMore.setOnClickListener(LoadMore); categories = (ListView) rootView.findViewById(R.id.lvCategories); categories.addFooterView(footer); if (categoryAdapter == null) { new LoadAllProducts("1").execute(); categoryAdapter = new CategoryAdapter(getActivity(), list, mImageLoader); categories.setAdapter(categoryAdapter); } else { categories.setAdapter(categoryAdapter); categories.onRestoreInstanceState(state); } return rootView; } View.OnClickListener LoadMore = new View.OnClickListener() { @Override public void onClick(View v) { new LoadAllProducts("1").execute(); } }; @Override public void onPause() { state = categories.onSaveInstanceState(); super.onPause(); categories.removeFooterView(footer); } class LoadAllProducts extends AsyncTask<String, String, String> { private String getParams; private JSONArray products; private JSONObject json; private JSON jParser = new JSON(); public LoadAllProducts(String string) { getParams = string; } protected String doInBackground(String... strings) { json = jParser.makeHttpRequest("http://url.getallcats.php", getParams); return null; } protected void onPostExecute(String string) { try { products = json.getJSONArray("category"); for (int i = 0; i < products.length(); i++) { JSONObject c = products.getJSONObject(i); list.add(new Construct( c.getString("name"), c.getString("count"), c.getInt("have"), "http://url.ru/img/" + c.getString("image"))); } } catch (JSONException e) { e.printStackTrace(); } categoryAdapter.notifyDataSetChanged(); Toast.makeText(getActivity(), "Ok", Toast.LENGTH_SHORT).show(); // срабатывает } } 

Implementing a CategoryAdapter adapter

 public class CategoryAdapter extends BaseAdapter { private List<Construct> list; private LayoutInflater inflater; private ImageLoader mImageLoader; public CategoryAdapter(Context context, List<Construct> list, ImageLoader mImageLoader) { this.list = list; this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); this.mImageLoader = mImageLoader; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = inflater.inflate(R.layout.item_category, null); holder = new ViewHolder(); convertView.setTag(holder); holder.ImageProductView = (ImageView) convertView.findViewById(R.id.ImageProductView); holder.tvPost = (TextView) convertView.findViewById(R.id.tvPost); holder.productId = (TextView) convertView.findViewById(R.id.textView); } else { holder = (ViewHolder) convertView.getTag(); } holder.tvPost.setText(list.get(position).getCategoryTitle()); holder.productId.setText(list.get(position).getProductId()); // IMAGE LOADER mImageLoader.displayImage(list.get(position).getProductImage(), holder.ImageProductView, null, new ImageLoadingListener() { final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>()); @Override public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { if (loadedImage != null) { ImageView imageView = (ImageView) view; boolean firstDisplay = !displayedImages.contains(imageUri); if (firstDisplay) { displayedImages.add(imageUri); } else { FadeInBitmapDisplayer.animate(imageView, 200); } } } @Override public void onLoadingCancelled(String uri, View view) { } @Override public void onLoadingStarted(String uri, View view) { FadeInBitmapDisplayer.animate(view, 0); } @Override public void onLoadingFailed(String uri, View view, FailReason fail) { } }); return convertView; } private class ViewHolder { public ImageView ImageProductView; public TextView tvPost; public TextView productId; } } 

And also Construct

 public class Construct { // товар private String productTitle; private String productId; private String productImage; private int productRating; // категория private String categoryTitle; public Construct(String title, String id, int rate, String image) { this.productTitle = title; this.productId = id; this.productImage = image; this.productRating = rate; this.categoryTitle = title; } // товар public String getProductTitle() { return productTitle; } public String getProductId() { return productId; } public String getProductImage() { return productImage; } public int getProductRating() { return productRating; } // категория public String getCategoryTitle() { return categoryTitle; } } 
  • show the implementation of the CategoryAdapter constructor - SorryForMyEnglish
  • @SorryForMyEnglish Added CategoryAdapter and Construct , can there be a problem in the adapter too? Before the fragment is changed back and forth, everything works fine .. - shurakana
  • Add an add method for the adapter and work with it. The couple seems to me after restoring the adapter list and fragment list different objects - SorryForMyEnglish
  • @SorryForMyEnglish I don’t quite understand what you are saying, I have only been working for a week with applications, before this php and js. It still seems to me that the problem is different, the same construction works fine if you do full activation and not fragments, only there you do not have to use onRestoreInstanceState(state) , since there and so everything is saved, can this be the cause of some "freezing"? - shurakana
  • Why do you manually call onSaveInstanceState / onRestoreInstanceState and save the state to state? - lsillarionov

1 answer 1

If you need to upload data to the adapter each time, transfer it from onCreateView to onResume:

 @Override public void onResume(){ super.onResume(); new LoadAllProducts("1").execute(); categoryAdapter = new CategoryAdapter(getActivity(), list, mImageLoader); categories.setAdapter(categoryAdapter); } 
  • No, you don’t need to load data, just by pressing a button, and after returning from another fragment, the adapter restores its position and number of elements, but for some reason it does not load data anymore, rather loads it (probably because the toast works) but the adapter is not updated. - shurakana