Search code examples
javaandroidimagelistviewsearchbar

When click searchbar it the value below come up


its image showing value and search bar

it is the image showing when clicking the search bar it will come up the values in list-view

I want to know why this happen and how to avoid the situation please help me thanks in advance

public class ListItem extends AppCompatActivity
{
    ListView listView1;
    ArrayList<String> listproduct;
    ArrayAdapter<String > adapternew;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listitem);
        ActionBar actionBar=getSupportActionBar();
        actionBar.setTitle("Please Select the Item");
      SearchView search=(SearchView)findViewById(R.id.Search);
        ScrollView scroll=(ScrollView)findViewById(R.id.Scroll);
        ListView listView1=(ListView)findViewById(R.id.Listview);
        listproduct=new ArrayList<>();
        listproduct.add("Stove Top Stuffing");
        listproduct.add("Campell's Soup");
        listproduct.add("Tide");
        listproduct.add("Pampers");
        listproduct.add("Pepsi Products");
        listproduct.add("Tang");
        listproduct.add("Top Ramen");
        listproduct.add("Knorr");
        listproduct.add("Palmolive");
        listproduct.add("Scotch-Brite");
        listproduct.add("Bounty Paper Towls");
        listproduct.add("Oreo Cookies");
        listproduct.add("Quaker Oats");
        listproduct.add("Lays Potato Chips");
        adapternew = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,listproduct);
        listView1.setAdapter(adapternew);
        SearchManager manager=(SearchManager)getSystemService(Context.SEARCH_SERVICE);
        search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                search.clearFocus();
            if(listproduct.contains(query))
            {
            adapternew.getFilter().filter(query);
            }
                return false;
            }
            @Override
            public boolean onQueryTextChange(String newText) {
                adapternew.getFilter().filter(newText);
                return false;
            }
             });
          listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int newposition, long id) {
                Toast.makeText(ListItem.this,listproduct.get(newposition)+"",Toast.LENGTH_SHORT).show();
                Intent i=new Intent(ListItem.this,AddOrder.class);
                i.putExtra("position",listproduct.get(newposition));
                //adapternew.notifyDataSetChanged();
                 startActivity(i);
            }
        });



    }
}

here is the code that is to show the list view item and i want to know whether the value in list view come up when clicking the search bar

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">


    <SearchView
        android:id="@+id/Search"

        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_alignParentTop="true"
        android:iconifiedByDefault="false"
        android:queryHint="Search Here"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.00" />


    <ScrollView
        android:id="@+id/Scroll"
        android:layout_width="100dp"
        android:layout_height="150dp"
        tools:layout_editor_absoluteX="-8dp"
        tools:layout_editor_absoluteY="396dp">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

        </LinearLayout>
    </ScrollView>

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar2"
        android:layout_width="match_parent"
        android:layout_height="5dp"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="?attr/actionBarTheme"
        tools:layout_editor_absoluteX="9dp"
        tools:layout_editor_absoluteY="2dp" />

    <ListView
        android:id="@+id/Listview"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_below="@id/Search"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.100"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.200">

    </ListView>
</androidx.constraintlayout.widget.ConstraintLayout>

and this is the xml file of the listview


Solution

  • I think this is the code you are wanting.

    No need to use scrollview here because ListView itself is scrollable. LinearLayout with Vertical Orientation will solve your issue

    <?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"
    android:orientation="vertical">
    
    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar2"
        android:layout_width="match_parent"
        android:layout_height="5dp"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="?attr/actionBarTheme"
        tools:layout_editor_absoluteX="9dp"
        tools:layout_editor_absoluteY="2dp" />
    
    
    <SearchView
        android:id="@+id/Search"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_alignParentTop="true"
        android:iconifiedByDefault="false"
        android:queryHint="Search Here"/>
    
    <ListView
        android:id="@+id/Listview"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_marginTop="10dp">
    
    </ListView>
    </LinearLayout>