首页 > 解决方案 > 当单击搜索栏时,会出现以下值

问题描述

它的图像显示价值和搜索栏

这是单击搜索栏时显示的图像,它将在列表视图中显示值

我想知道为什么会发生这种情况以及如何避免这种情况请帮助我提前谢谢

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);
            }
        });



    }
}

这是显示列表视图项的代码,我想知道单击搜索栏时列表视图中的值是否出现

<?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>

这是列表视图的 xml 文件

标签: javaandroidimagelistviewsearchbar

解决方案


我认为这是您想要的代码,如果我错了请纠正我不需要在scrollview这里使用,因为ListView它本身是可滚动的并且具有垂直方向的 LinearLayout 将解决您的问题

<?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>

推荐阅读