0
我一直在尋找如何使ListView
可以在SearchView
內過濾。 但現在我試圖找出如何連接到基於最高結果的活動。問題與活動和過濾ListView
我試過這段代碼,但仍然得到相同的活動但不同的搜索結果。
package com.rahmadewi30.android.kamus;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;
public class Search extends AppCompatActivity implements SearchView.OnQueryTextListener {
ListView lvItem;
ArrayAdapter<String> adapter;
SearchView inputSearch;
String[] tumbuhan = new String[]{
"Lumut Tanduk","Lumut Hati","Lumut Daun" ,"Semanggi", "Bakau",
"Paku Kawat", "Nanas","Pisang","Mangga"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
lvItem = (ListView) findViewById(R.id.listItem);
inputSearch = (SearchView) findViewById(R.id.editCari);
adapter = new ArrayAdapter<>(Search.this,
android.R.layout.simple_list_item_1, android.R.id.text1, tumbuhan);
inputSearch.setOnQueryTextListener(this);
lvItem.setAdapter(adapter);
lvItem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {
Intent l = new Intent(view.getContext(), ActivityLumut.class);
startActivityForResult(l, 0);
} else if (position == 1) {
Intent l = new Intent(view.getContext(), ActivityLumutHati.class);
startActivityForResult(l, 1);
} else if (position == 2) {
Intent l = new Intent(view.getContext(), ActivityLumutDaun.class);
startActivityForResult(l, 2);
}
}
});
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return false;
}
}
點擊('如果(位置== 0){'...)總會啓動相同的活動,不管是什麼item('view')實際上是/ contains。 – Gooz
是的,就是這樣!你能幫我解決嗎? –