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

戏子无情 提交于 2019-12-24 07:49:42

问题


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

MainActivity: The activity that hosts the GridView which has the list of products,

ProductDetailActivity: The activity that shows the details of the selected item in GridView after the click,

ProductMainAdapter: The adapter class for product data in GridView.

The workflow: 

1 - The user opens the app, main activity starts (launchMode="singleTask"), with a list of products queried from server using Volley, and listed in a GridView. The queried data include productIDs and images only.

2 - User clicks on any product (item) on GridView and the selected productID is used to get detailed information from server (Volley) and opening the ProductDetailActivity with the intent that has selected product data.

3 - User sees the details and turns back to MainActivity and selects another item. And goes on like that.

This workflow works perfectly in %95 of users` devices. But every once in a while I get a message from a user which in summary says: "The first clicked product keeps opening in subsequent clicks". For example, user clicks on item X, and the details of X opens. Then user goes to main and clicks on item Y, but item X is opening again. I tested it in emulator with all the available Android versions from 15 to 25 over the past few weeks. I also tested it with all the devices that I can find around me, but could not reproduce the issue. A user sent a video showing the problem and it is a genuine issue.

The relevant code snippet in the Adapter is as follows:

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 is:

protected void onResume() {
        super.onResume();
        ...

        productID = getIntent().getStringExtra("pid");
        jsonObject = getIntent().getStringExtra("jsonObject");

...
}

Since I am not able to reproduce the issue, I could not debug it. I could have asked a user to give me their phone for testing, they live in another country.

Either GridView is caching the first click and keeps returning it, or the product detail activity cashes its data for some reason. I tried using onItemClickListener() of GridView but did not work. I also tried sending only the productID to the detail activity and Volleying the details from the new activity. That did not work either. Have anyone ever faced a similar situation? Any suggestion or comment is highly appreciated.

Thank you

来源:https://stackoverflow.com/questions/41195367/android-gridview-opens-the-same-entry-the-first-clicked-one-in-subsequent-cli

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!