Change the text in search view when item in listview is clicked

Viewed 329

When I write query in Searchview a list of items(Suggestions list) is apppeared and filtered as per the QueryText changed. When i click the list item the text of filtered list does not appear in the search view.

I want the filtered list item to appear in the search view when clicked. But Here the Item of the unfiltered list appears when i click the filtered list

How can I Solve this problem?

Xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MySearchView"
    android:orientation="vertical"
    android:padding="30dp">

    <SearchView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:queryHint="Search here..."
        android:iconifiedByDefault="false"
        android:focusedByDefault="true"
        android:transitionName="Search_box"
        android:id="@+id/search_view">
    </SearchView>


    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:id="@+id/list_view">

    </ListView>
</LinearLayout>

java file:

package edmt.dev.androidgridlayout;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.Toast;

import com.androidnetworking.AndroidNetworking;
import com.androidnetworking.common.Priority;
import com.androidnetworking.error.ANError;
import com.androidnetworking.interfaces.JSONArrayRequestListener;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

import edmt.dev.androidgridlayout.HelperClasses.HomeAdapter.FeaturedHelperClass;
import edmt.dev.androidgridlayout.HelperClasses.HomeAdapter.ProductAdapter;

public class MySearchView extends AppCompatActivity {
    SearchView searchView;
    ListView listView;

    ArrayList<String> list,catid;
    ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_search_view);

        searchView=findViewById(R.id.search_view);
        searchView.requestFocusFromTouch();
        listView=findViewById(R.id.list_view);
        list =new ArrayList<String>();
        catid =new ArrayList<String>();

        String url = getResources().getString(R.string.url);
        String SelectCategory = url + "request=SelectCategory";
        System.out.println(SelectCategory);
        AndroidNetworking.get(SelectCategory)
                .setPriority(Priority.LOW)
                .build()
                .getAsJSONArray(new JSONArrayRequestListener() {
                    @Override
                    public void onResponse(JSONArray response) {
                        JSONObject jo;
                        try {
                            for (int i = 0; i < response.length(); i++) {
                                jo = response.getJSONObject(i);
                                String sid = jo.getString("id");
                                String sname = jo.getString("title");
                                list.add(sname);
                                catid.add(sid);

                            }
                            adapter=new ArrayAdapter<>(getApplicationContext(),R.layout.dropdown_item,list);
                            listView.setAdapter(adapter);


                        } catch (Exception e) {
                            Toast.makeText(getApplicationContext(), "JSON Error" + e.getMessage(), Toast.LENGTH_LONG).show();
                        }
                    }

                    @Override
                    public void onError(ANError anError) {
                        anError.printStackTrace();
                        Toast.makeText(getApplicationContext(), "unsuccessful : error is :" + anError.getMessage(), Toast.LENGTH_LONG).show();
                    }
                });



        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                Intent intent=new Intent(getApplicationContext(),AllProductPage.class);
                intent.putExtra("categoryid","0");
                intent.putExtra("categorytitle",query);
                intent.putExtra("request","MySearchView");
                startActivity(intent);
                Toast.makeText(getApplicationContext(),"searchview"+query,Toast.LENGTH_SHORT).show();
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                adapter.getFilter().filter(newText);
                return false;
            }
        });

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

                searchView.setQuery(list.get(position),false);

            }
        });

    }
}
0 Answers
Related