Android: GridView opens the same record (first clicked) in subsequent clicks

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

MainActivity: operation that hosts a GridView that has a list of products,

ProductDetailActivity: activity that shows the details of the selected item in the GridView after a click.

ProductMainAdapter: Adapter class for product data in a GridView .

 The workflow: 

1 - the user opens the application, the main launch begins ( launchMode="singleTask" ), a list of products requested from the server using Volley , and is specified in the GridView . The requested data includes only product identifiers and images.

2 - The user clicks on any product (element) on the GridView , and the selected product identifier is used to obtain detailed information from the server ( Volley ) and open the ProductDetailActivity with the intent that selected the product data.

3 - The user sees the details and returns to MainActivity and selects another item. And go on like this.

This workflow works great on% 95 user devices. But every time from time to time I get a message from the user, which briefly says: "The first clicked product continues to open on subsequent clicks." For example, the user clicks on an element X, and the details of X open. Then the user goes to the main one and clicks on the element Y, but the element X opens again. I tested it in an emulator with all available versions of Android from 15 to 25 over the past few weeks. I also tested it with all the devices that can be found around me, but could not reproduce the problem. The user posted a video showing the problem, and this is the real problem.

The corresponding code snippet in Adapter looks like this:

 public View getView(final int position, View convertView, @NonNull ViewGroup parent) { ... holder.imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { openProductDetail(position); } }); ... } private void openProductDetail(int position) { if(!Connectivity.isConnected(getContext())) { CommonUtil.buildDialog(getContext(), getContext().getResources().getString(R.string.no_network_title), getContext().getResources().getString(R.string.no_network_msg)).show(); return; } try { ImageRecord imr = getItem(position); String productID = imr != null ? imr.getId() : "0"; getProductDetail(productID); } catch (Exception e) { Log.e("openProductDetail", "exception", e); } } private void getProductDetail(final String productID) { ... StringRequest postRequest = new StringRequest(Request.Method.POST, Config.PRODUCT_DETAIL_URL, new Response.Listener<String>() { @Override public void onResponse(String response) { try { if(response != null && !response.equals("")) { Intent intent = new Intent(getContext(), ProductDetailActivity.class); intent.putExtra("pid", productID); intent.putExtra("jsonObject", response); getContext().startActivity(intent); } } 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(getContext()).getRequestQueue().add(postRequest); } 

And the ProductDetailActivity code:

 protected void onResume() { super.onResume(); ... productID = getIntent().getStringExtra("pid"); jsonObject = getIntent().getStringExtra("jsonObject"); ... } 

Since I cannot reproduce the problem, I could not debug it. I could ask the user to give me their phone for testing, they live in another country.

Either GridView caches the first click and continues to return it, or the activity of the product part caches its data for some reason. I tried using onItemClickListener() from the GridView , but did not work. I also tried sending only the productID to the detailed activity and involving details from the new activity. That didn't work either. Has anyone encountered a similar situation? Any suggestion or comment is greatly appreciated.

thanks

+1
source share

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


All Articles