[Android Studio] arindam4u:
I need help for filtering a listview and open a new activity from the filtered list view onItemClick. The problem is I get the filtered view right but I do not know how to store the ID from JSON for each post.

Problem I am facing: When I click the list item when it is not yet filtered it passes the correct ID for the selected list item. But when I try to filter it, it is showing wrong data (data of another item) in the next activity.

What I am trying to achieve: to show a list of titles in a listview which can be filtered with an edit text box. when I click on any list item (if it is filtered or not) it will pass the ID of that respective item to the next activity.

The Main Fragment

- From where I am fetching JSON data and listing it in a listview and filtering the data with edit text.

public class EnglishHomeSearchFragment extends Fragment {

String url = "MYJSONURL/posts?categories=4&fields=id,title";
List list;
Gson gson;
ProgressBar progressBar;
ListView postList;
Map < String, Object > mapPost;
Map < String, Object > mapTitle;
int postID;
String postTitle[];
EditText editText;
private ArrayAdapter adapter;
private View rootView;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.search_englishhome, container, false);
if (!isConnected(getActivity())) buildDialog(getActivity()).show();
else {
postList = (ListView) rootView.findViewById(R.id.postList);
progressBar = (ProgressBar) rootView.findViewById(R.id.progressBar);
progressBar.setVisibility(View.VISIBLE);
initlist();
editText = (EditText) rootView.findViewById(R.id.inputsearchenglishhome);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence cs, int start, int count, int after) {

}

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

System.out.println("Text[" + s + "]");
adapter.getFilter().filter(s.toString());

}

@Override
public void afterTextChanged(Editable s) {

}
});
}
return rootView;
}

private void initlist() {

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) {
Toast.makeText(getActivity(), "Error! Check your Internet Connection.", Toast.LENGTH_LONG).show();
}
});
RequestQueue rQueue = Volley.newRequestQueue(rootView.getContext());
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(), EnglishPost.class);
intent.putExtra("id", "" + postID);
startActivity(intent);
}
});

}

public boolean isConnected(Context context) {

ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netinfo = Objects.requireNonNull(cm).getActiveNetworkInfo();

if (netinfo != null && netinfo.isConnectedOrConnecting()) {
android.net.NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo mobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

return (mobile != null && mobile.isConnectedOrConnecting()) || (wifi != null && wifi.isConnectedOrConnecting());
} else return false;
}

public AlertDialog.Builder buildDialog(Context c) {

AlertDialog.Builder builder = new AlertDialog.Builder(c);
builder.setTitle("No Internet connection.");
builder.setMessage("You are not connected to the Internet.");

builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {

dialog.dismiss();

}
});

return builder;
}

}

I have asked for help in several help forums. But no one helped me yet. I am new to Android app development. Please help me to resolve this issue without changing the whole code. Thank you in advance. :)

My JSON data looks like this:

[
{
id: 543,
title: {
rendered: "We are chopped down"
}
},
{
id: 535,
title: {
rendered: "Recover"
}
},
{
id: 528,
title: {
rendered: "Teacher said : Explain the law of gravitation."
}
},
{
id: 517,
title: {
rendered: "Person who ordered Swami to dress up and go to school __________?"
}
},
{
id: 514,
title: {
rendered: "Time when Swami complained of a headache _________?"
}
},
{
id: 512,
title: {
rendered: "When Swami ought to have been in the school prayer hall, he was lying on the?"
}
},
{
id: 510,
title: {
rendered: "Put out"
}
},
{
id: 505,
title: {
rendered: "Make out"
}
},
{
id: 502,
title: {
rendered: "Why did not Swami go to School?"
}
},
{
id: 484,
title: {
rendered: "With a shudder Swami realized that it was?"
}
}
]