我需要知道我是否可以搜索搜索结果。例如,如果我有项目的列表视图(香蕉奶昔,香蕉冰淇淋,苹果汁,香蕉汁,苹果派等),我在我的搜索视图中输入香蕉,所以结果是(香蕉奶昔,香蕉冰淇淋,香蕉汁)和比我之前的搜索下面还有另外一个搜索,我输入了JUICE,它会弹出BANANA JUICE。我希望你理解我,我希望有人能迅速回答我这个问题。这是我的代码的样子。 BTW我做了2个搜索视图,当我尝试这样做时结果是(APPLE JUICE,BANANA JUICE)beucase搜索之间没有联系。 请帮助。
public class recepies extends Activity {
ListView lv;
SearchView sv;
SearchView sv2;
String[] recepies={"Čili Piletina\n"+ " pileće belo meso \n"+" crvena paprika \n"+" čili papričica \n"+" crveni pasulj \n"+" kukuruzni šećer \n"+" paradajz pelat\n"+" kisela pavlaka\n"+ " biljni začin\n "+ " biber\n "+" ulje ",
"Prženija sa svinjskim butom\n" +" svinjski but\n"+" kiseli krastavčići\n , kisala paprika\n,tucana paprika,biljni začin,biber,origano ulje, so, lovorov list",
"Peketići od integralnih palačinki\n" + "integralno brašno ,voda, soda bikarbona, ulje, kisela pavlaka, krastavac, paradajz, kikiriki",
"Ćufte od mesa i karfiola (hrono recept)\n" + "mleveno meso, karfiol, crni luk, beli luk, peršun, belance, so, biber, kurkuma, ulje, tikvice, sok od paradajza",
"Paprika punjena rižom i piletinom\n" +"paprika babura,belo meso,šargarepa,crni luk,pirinač,paradajz,sok od paradajza,biljnog začina,so,biber, kari,peršun, maslinovo ulje,",
"Piletina u sosu od nara\n" +"belo meso,\n" + "soja sos,\n" + "biber,\n" + "kurkuma,\n" + "luk,\n" + "soka od nara,\n" + "đumbir,\n" + "pirinač,\n" + "čili ili tabasko sos,\n" + "bbq ili roštilj sos,"
};
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recepies);
registerClickCallback();
lv=(ListView) findViewById(R.id.listView);
sv=(SearchView) findViewById(R.id.searchView1);
sv2=(SearchView) findViewById(R.id.searchView2);
adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,recepies);
lv.setAdapter(adapter);
sv2.setOnQueryTextListener(new OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String text) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onQueryTextChange(String text) {
adapter.getFilter().filter(text);
return false;
}
});
sv.setOnQueryTextListener(new OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String text) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onQueryTextChange(String text) {
adapter.getFilter().filter(text);
return false;
}
});
}
private void registerClickCallback(){
lv=(ListView) findViewById(R.id.listView);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) {
TextView textView= (TextView) viewClicked;
if(position==0){
goToRecepie1();
}else if(position==1){
goToRecepie2();
}else if(position==2){
goToRecepie3();
}else if(position==3){
goToRecepie4();
}else if(position==4){
goToRecepie5();
}else if(position==5){
goToRecepie6();
}
}
});
}
private void goToRecepie6() {
Intent intent = new Intent(this, Recepie6.class);
startActivity(intent);
}
private void goToRecepie5() {
Intent intent = new Intent(this, Recepie5.class);
startActivity(intent);
}
private void goToRecepie4() {
Intent intent = new Intent(this, Recepie4.class);
startActivity(intent);
}
private void goToRecepie3() {
Intent intent = new Intent(this, Recepie3.class);
startActivity(intent);
}
private void goToRecepie2() {
Intent intent = new Intent(this, Recepie2.class);
startActivity(intent);
}
private void goToRecepie1() {
Intent intent = new Intent(this, Recepie1.class);
startActivity(intent);
}
}
答案 0 :(得分:2)
每次扫描要扫描的列表中的每个元素。如果使用数组,则只需检查第n个元素是否包含要搜索的单词
编辑:必须取出&#34;是&#34;
答案 1 :(得分:2)
这是一个不使用XML的示例。
List<String> menu = new ArrayList<String>();
List<String> filtered = new ArrayList<String>();
String found;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
menu.add("APPLE JUICE");
menu.add("BANANA JUICE");
menu.add("MANGO JUICE");
menu.add("BANANA SMOOTHIE");
menu.add("PINEAPPLE JUICE");
menu.add("APPLE CIDER");
menu.add("BANANA MILKSHAKE");
for (int i =0;i < menu.size();i++)
{
if (menu.get(i).contains("BANANA"))
{
filtered.add(menu.get(i));
Log.i("AppInfo","1st Filter Round: "+menu.get(i));
}
}
for (int j = 0; j < filtered.size(); j++)
{
if (filtered.get(j).contains("JUICE"))
{
found = filtered.get(j);
Log.i("AppInfo","Final Found: "+found);
}
}
}
只需用用户输入的字符串替换for循环中的if语句字符串。我希望这有帮助