Volleyball works on WIFI, but not on 3G

I apologize in advance if the question is too long. Here we go:

There are two actions: Main and Detail Activity.

Main Activity is basically a GridView. Detail Activity basically shows the details of the clicked item. I pass the selected id ( pid ) element from Main to Detail Activity.

I ran into a problem as follows. First I have a 3G connection (cellular) and clicked on the first element, and see the corresponding detail in the Detail Activity , it works fine and returns to the Main Activity , and then clicked on the second element, then, unfortunately, it still shows me the first element in DetailActivity that I clicked at first.

I switched from 3G to wifi while the application is open and open. No matter what I click, it still shows me the first element that I clicked at first.

But when I uninstall the application and reinstall it and get either wifi access only , the application works fine.

In the next implementation, the connection URL (PRODUCT_DETAIL_URL) is http , not https . I am using the Volley library for network connectivity.

DetailActivity.java

 private void productDetailInit() { // it is http StringRequest postRequest = new StringRequest(Request.Method.POST, Config.PRODUCT_DETAIL_URL, new Response.Listener<String>() { @Override public void onResponse(String response) { try { jsonObject = response; loadJsonData(); } catch (Exception e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { error.printStackTrace(); } } ) { @Override protected Map<String, String> getParams() { Map<String, String> params = new HashMap<>(); params.put("id", productID); return params; } }; RetryPolicy policy = new DefaultRetryPolicy(1000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT); postRequest.setRetryPolicy(policy); CustomVolleyRequest.getInstance(this).getRequestQueue().add(postRequest); } 

CustomVolleyRequest.java

 public class CustomVolleyRequest { private static CustomVolleyRequest customVolleyRequest; private static Context context; private RequestQueue requestQueue; private ImageLoader imageLoader; private CustomVolleyRequest(Context context) { this.context = context; this.requestQueue = getRequestQueue(); imageLoader = new ImageLoader(requestQueue, new ImageLoader.ImageCache() { private final LruCache<String, Bitmap> cache = new LruCache<String, Bitmap>(20); @Override public Bitmap getBitmap(String url) { return cache.get(url); } @Override public void putBitmap(String url, Bitmap bitmap) { cache.put(url, bitmap); } }); } private class BitmapCache implements ImageLoader.ImageCache { private LruCache<String, Bitmap> mCache; public BitmapCache() { mCache = new LruCache<>(20); } @Override public Bitmap getBitmap(String url) { return mCache.get(url); } @Override public void putBitmap(String url, Bitmap bitmap) { // scaling bitmap for avoiding too much big images Bitmap scaled = ImageUtil.getInstance().scaleBitmap(bitmap); mCache.put(url, scaled); } } public static synchronized CustomVolleyRequest getInstance(Context context) { if (customVolleyRequest == null) { customVolleyRequest = new CustomVolleyRequest(context); } return customVolleyRequest; } public RequestQueue getRequestQueue() { if (requestQueue == null) { Cache cache = new DiskBasedCache(context.getCacheDir(), 10 * 1024 * 1024); Network network = new BasicNetwork(new HurlStack()); requestQueue = new RequestQueue(cache, network); requestQueue.start(); } return requestQueue; } public ImageLoader getImageLoader() { return imageLoader; } } 

Adapter.java

  class ProductMainAdapter extends ArrayAdapter<ImageRecord> { private ImageLoader mImageLoader; private String jsonObject; ProductMainAdapter(Context context) { super(context, R.layout.grid_item); mImageLoader = CustomVolleyRequest.getInstance(this.getContext()).getImageLoader(); } @NonNull @Override public View getView(final int position, View convertView, @NonNull ViewGroup parent) { final ViewHolder holder; if(convertView == null) { holder = new ViewHolder(); convertView = LayoutInflater.from(getContext()).inflate(R.layout.grid_item, parent, false); convertView.setBackgroundResource(R.drawable.round_gridview); holder.priceTagImage = (ImageView) convertView.findViewById(R.id.priceTag_IV); holder.textView = (TextView) convertView.findViewById(R.id.text); holder.imageView = (NetworkImageView) convertView.findViewById(R.id.picture); holder.priceTagRL = (RelativeLayout) convertView.findViewById(R.id.priceTag_RL); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } ImageRecord imageRecord = getItem(position); holder.imageView.setImageUrl(imageRecord != null ? imageRecord.getUrl() : null, mImageLoader); holder.imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { openProductDetail(position); } }); holder.textView.setText(imageRecord != null ? imageRecord.getTitle() : null); holder.priceTagRL.setRotation(0); return convertView; } private class ViewHolder{ TextView textView; ImageView priceTagImage; NetworkImageView imageView; RelativeLayout priceTagRL; } private void openProductDetail(int position) { try { ImageRecord imr = getItem(position); String productID = imr != null ? imr.getId() : "0"; Intent intent = new Intent(getContext(), ProductDetailActivity.class); intent.putExtra("pid", productID); getContext().startActivity(intent); } catch (Exception e) { Log.e("openProductDetail", "exception", e); } } 

I wonder what I do not see / do wrong in the provided implementation. Almost 2-3 months passed, I could not cope with this problem. Has anyone encountered a similar situation? Any suggestion or comment is greatly appreciated.

+5
source share
3 answers

You can simply kill actions with finish (); when another loads.

+2
source

I had the same problem too. In my case, in the onClick method onClick position was wrong. Then I used to set the position as a tag for a specific view that OnClickListener has, and my problem is solved. This may be due to the fact that the position is declared as final . So delete the last keyword and try like this.

 holder.imageView.setImageUrl(imageRecord != null ? imageRecord.getUrl() : null, mImageLoader); holder.imageView.setTag(position); holder.priceTagRL.setRotation(0); 

And in the onClick method

 openProductDetail((int)view.getTag()); 
0
source

Check if the link below can be useful:

fooobar.com/questions/500728 / ... (maybe the 3G connection will be slow to increase the timeout threshold)

In addition, there is a possibility that the link to the position in the adapter may be mistaken in some marginal case (regardless of your actual problem). You can try the following alternative method.

Use gridView.setOnItemClickListener

 gridview.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { //do your logic based on the position } }); 

Note. If the GridView cannot observe the element's click event, you must set android:descendantFocusability="blocksDescendants" to the root view of the child element in the gridview. This can happen mainly because the elements of the GridView should not be clickable on their own, as they will observe the click event.

Link: https://developer.android.com/guide/topics/ui/layout/gridview.html

0
source

Source: https://habr.com/ru/post/1261435/


All Articles