Android ListView 和 SearchView,过滤后的搜索结果没有指向正确的意图
Android ListView with SearchView, Filtered search results do not direct ti the right intent
首先我是初学者所以请耐心等待。
我正在尝试创建一个包含项目列表的应用程序。当您单击该项目时,您将转到一个新的对应 activity。
当我添加一个 SearchView 来过滤列表项时,现在的问题是点击时过滤结果不会指向相应的 activity.
能否请您更正我下面的代码或提供更好的方法来定向到相应的 activity。请提供尽可能详细的信息。非常感谢您。
MainActivity.java
public class MainActivity extends Activity
implements SearchView.OnQueryTextListener {
private SearchView mSearchView;
private ListView mListView;
private final String[] mStrings = { "Google", "Apple", "Samsung", "Sony", "LG", "HTC" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.activity_main);
mSearchView = findViewById(R.id.search_view);
mListView = findViewById(R.id.list_view);
mListView.setAdapter(new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1,
mStrings));
mListView.setTextFilterEnabled(true);
setupSearchView();
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {
Intent i = new Intent(MainActivity.this, google.class);
startActivity(i);
} else {
// code to direct to the corresponding activity
}
});
}
private void setupSearchView() {
mSearchView.setIconifiedByDefault(false);
mSearchView.setOnQueryTextListener(this);
mSearchView.setSubmitButtonEnabled(true);
mSearchView.setQueryHint("Search Here");
}
public boolean onQueryTextChange(String newText) {
if (TextUtils.isEmpty(newText)) {
mListView.clearTextFilter();
} else {
mListView.setFilterText(newText);
}
return true;
}
public boolean onQueryTextSubmit(String query) {
return false;
}
}
if (position == 0) {
Intent i = new Intent(MainActivity.this, google.class);
startActivity(i);
} else if ( position == 1){
// do something, sending to activity of choice
} else if ( position == 2){
// do something , sending to activity of choice
}
重复此步骤以获得您想要的不同活动数量。
我是这样做的。我没有检查搜索过滤列表时更改的项目位置,而是检查字符串值。成功了
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String name = (String) mListView.getItemAtPosition(position);
if (name == "Google") {
Intent i = new Intent(MainActivity.this, Google.class);
startActivity(i);
} else {
// code to direct to the corresponding activity
}
});
首先我是初学者所以请耐心等待。
我正在尝试创建一个包含项目列表的应用程序。当您单击该项目时,您将转到一个新的对应 activity。 当我添加一个 SearchView 来过滤列表项时,现在的问题是点击时过滤结果不会指向相应的 activity.
能否请您更正我下面的代码或提供更好的方法来定向到相应的 activity。请提供尽可能详细的信息。非常感谢您。
MainActivity.java
public class MainActivity extends Activity
implements SearchView.OnQueryTextListener {
private SearchView mSearchView;
private ListView mListView;
private final String[] mStrings = { "Google", "Apple", "Samsung", "Sony", "LG", "HTC" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.activity_main);
mSearchView = findViewById(R.id.search_view);
mListView = findViewById(R.id.list_view);
mListView.setAdapter(new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1,
mStrings));
mListView.setTextFilterEnabled(true);
setupSearchView();
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {
Intent i = new Intent(MainActivity.this, google.class);
startActivity(i);
} else {
// code to direct to the corresponding activity
}
});
}
private void setupSearchView() {
mSearchView.setIconifiedByDefault(false);
mSearchView.setOnQueryTextListener(this);
mSearchView.setSubmitButtonEnabled(true);
mSearchView.setQueryHint("Search Here");
}
public boolean onQueryTextChange(String newText) {
if (TextUtils.isEmpty(newText)) {
mListView.clearTextFilter();
} else {
mListView.setFilterText(newText);
}
return true;
}
public boolean onQueryTextSubmit(String query) {
return false;
}
}
if (position == 0) {
Intent i = new Intent(MainActivity.this, google.class);
startActivity(i);
} else if ( position == 1){
// do something, sending to activity of choice
} else if ( position == 2){
// do something , sending to activity of choice
}
重复此步骤以获得您想要的不同活动数量。
我是这样做的。我没有检查搜索过滤列表时更改的项目位置,而是检查字符串值。成功了
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String name = (String) mListView.getItemAtPosition(position);
if (name == "Google") {
Intent i = new Intent(MainActivity.this, Google.class);
startActivity(i);
} else {
// code to direct to the corresponding activity
}
});