gpt4 book ai didi

java - 如何隐藏 ListView,直到在 SearchView 中输入字符串

转载 作者:行者123 更新时间:2023-12-01 21:45:31 25 4
gpt4 key购买 nike

我试图隐藏我的 ListView,直到在 SearchView 中输入字符串。

我对 Java 很陌生,但我尝试了文本监听器,甚至妥协了单击方法,但我似乎无法使其工作。

package com.example.searchfunction;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SearchView;
import java.util.ArrayList;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener {

// Declare Variables
ListView list;
ListViewAdapter adapter;
SearchView editsearch;
String[] animalNameList;
ArrayList<AnimalNames> arraylist = new ArrayList<AnimalNames>();

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Generate sample data
animalNameList = new String[]{"Lion", "Tiger", "Dog",
"Cat", "Tortoise", "Rat", "Elephant", "Fox",
"Cow","Donkey","Monkey"};

// Locate the ListView in listview_main.xml
list = findViewById(R.id.listview);

for (int i = 0; i < animalNameList.length; i++) {
AnimalNames animalNames = new AnimalNames(animalNameList[i]);
// Binds all strings into an array
arraylist.add(animalNames);
}

// Pass results to ListViewAdapter Class
adapter = new ListViewAdapter(this, arraylist);

// Binds the Adapter to the ListView
list.setAdapter(adapter);

// Locate the EditText in listview_main.xml
editsearch = (SearchView) findViewById(R.id.search);
editsearch.setOnQueryTextListener(this);
}

@Override
public boolean onQueryTextSubmit(String query) {

return false;
}

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

最佳答案

在它应该起作用的方法中返回true


@Override
public boolean onQueryTextChange(String newText) {
String text = newText;
adapter.filter(text);
if(TextUtils.isEmpty(text)){
list.setVisibility(View.GONE);
}
else {
list.setVisibility(View.VISIBLE);
}
return true;
}

关于java - 如何隐藏 ListView,直到在 SearchView 中输入字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58782426/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com