Click here to Skip to main content
15,888,066 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
For a couple of days, I am trying to achieve a simple task. I want to filter a listview and when I click on the filtered list item, the OnItemClickListener collect the postID from the desired list item and put that value to next activity.

But the problem is, the onItemClickListener is not able to get postID from the filtered position list, it appears that, it clicking the filtered list item but getting postID of old list item's position.

For Example, Say my listview at initial position looks like this:

A
AB
BA
C
CB


In this position, if I select any list item, it will get the postID of the correct item.

Now the problem arises when I filter the ListView with an edit text. Say, I typed C in my edit text. Then the ListView changes as follows:

C
CB


In this position, if I click on C, it will get the postID of A and if I click on CB it will get the postID of AB as per the initial list position.

What I have tried:

Please look at my fragment's code where the list is being generated:
Java
public void filterTextmethod() {

        editText = (EditText) rootView.findViewById(R.id.inputsearchenglishhome);

        initlist();

        editText.addTextChangedListener(new TextWatcher() {


            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {


            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {


                if (adapter != null) {
                    System.out.println("Text[" + s + "]");
                    adapter.getFilter().filter(s.toString());
                    adapter.notifyDataSetChanged();
                }


            }

            @Override
            public void afterTextChanged(Editable s) {


            }

        });

    }


    private void initlist() {


            progressBar.setVisibility(View.VISIBLE);
            StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {

                gson = new Gson();
                list = (List) gson.fromJson(s, List.class);
                postTitle = new String[list.size()];

                for (int i = 0; i < list.size(); ++i) {
                    mapPost = (Map<String, Object>) list.get(i);
                    mapTitle = (Map<String, Object>) mapPost.get("title");
                    postTitle[i] = (String) mapTitle.get("rendered");
                 }


                adapter = new ArrayAdapter<>(Objects.requireNonNull(getActivity()), android.R.layout.simple_list_item_1, postTitle);                
                postList.setAdapter(adapter);
                progressBar.setVisibility(View.GONE);                

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                progressBar.setVisibility(View.GONE);
                if (mToast != null) {
                    mToast.cancel();
                }
                mToast = Toast.makeText(getActivity(), "Error! Check your Internet Connection.", Toast.LENGTH_LONG);
                mToast.show();
            }
        });
                request.setRetryPolicy(new DefaultRetryPolicy(5000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
                RequestQueue rQueue = Volley.newRequestQueue(Objects.requireNonNull(getActivity()).getApplicationContext());
                rQueue.getCache().invalidate(url, true);
                rQueue.add(request);

                postList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                        mapPost = (Map<String, Object>) list.get(position);
                        postID = ((Double) mapPost.get("id")).intValue();

                        Intent intent = new Intent(getActivity(), FullPostView.class);
                        intent.putExtra("id", "" + postID);
                        //intent.putExtra("colorcode", "#FF5252");
                        //intent.putExtra("parentFragmentSubject", "English");
                        startActivity(intent);

                    }


                });

    }


Now look at the XML:

XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".English.EnglishHomeScreen"
    android:gravity="center"
    android:id="@+id/searchlayoutenglishhome">

    <ProgressBar
        android:id="@+id/progressBar"
        style="@style/RedAccent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:visibility="invisible"
        android:layout_marginTop="150dp"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/inputsearchenglishhome"/>

    <EditText
        android:id="@+id/inputsearchenglishhome"
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:layout_marginTop="10dp"
        android:drawableStart="@drawable/ic_search_black_24dp"
        android:drawableTint="@color/search_black"
        android:paddingStart="8dp"
        android:paddingEnd="8dp"
        android:drawablePadding="8dp"
        android:background="@drawable/edittext_round_red"
        android:hint="Search in all English Questions"
        android:layout_gravity="bottom"
        android:inputType="textVisiblePassword"/>


    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:layout_marginBottom="10dp"
        android:paddingBottom="10dp"
        android:id="@+id/postList"
        android:layout_below="@+id/inputsearchenglishhome"
        android:descendantFocusability="blocksDescendants"
        android:clickable="false"
        android:focusable="false"/>


</RelativeLayout>


Please help me to find the solution. i will really appreciate your help. I cannot figure out the problem from 2-3 days. Help me please. Thank you.
Posted
Updated 14-Sep-20 2:12am

1 solution

The position in onItemClick is an index of the adapter, e.g.
adapter.getItemAtPosition(position)

However you use it as an index for mapPost. It looks like you are filtering the adapter, but not mapPost.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900