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:
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);
startActivity(intent);
}
});
}
Now look at the 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.