如何使 SearchList 在 Android 中按字符串工作?
Posted
技术标签:
【中文标题】如何使 SearchList 在 Android 中按字符串工作?【英文标题】:How to make the SearchList work by String in Android? 【发布时间】:2016-03-10 19:41:27 【问题描述】:我有一个带有列表视图的搜索视图。我把一些项目打开一个新的活动。当我在第一个活动中时,好吧。但是,如果我在 Searchview 上搜索另一个项目,应用程序会服从该位置。然后,选择了错误的项目。 我怎样才能通过字符串使项目点击工作?
我的代码:
public class MainActivity extends ListActivity
ListView lv;
SearchView sv;
String[] teams="Activity 1","Activity 2","Activity 3","Activity 4","Activity 5","Activity 6";
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView) findViewById(android.R.id.list);
sv=(SearchView) findViewById(R.id.searchView);
adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,teams);
lv.setAdapter(adapter);
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener()
@Override
public boolean onQueryTextSubmit(String text)
return false;
@Override
public boolean onQueryTextChange(String text)
adapter.getFilter().filter(text);
return false;
);
@Override
//to open new activity
protected void onListItemClick(ListView l, View v, int position, long id)
super.onListItemClick(l, v, position, id);
if (position == 0)
Intent intent = new Intent(this, Main2Activity.class);
startActivity(intent);
else if (position == 1)
Intent intent = new Intent(this, Main3Activity.class);
startActivity(intent);
@Override
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
@Override
public boolean onOptionsItemSelected(MenuItem item)
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings)
return true;
return super.onOptionsItemSelected(item);
【问题讨论】:
【参考方案1】:我怎样才能通过字符串使项目点击工作?
@Override
//to open new activity
protected void onListItemClick(ListView l, View v, int position, long id)
super.onListItemClick(l, v, position, id);
String team = teams[position];
if (team.equalsIgnoreCase("Activity 1"))
Intent intent = new Intent(this, Activity1.class);
startActivity(intent);
else if (team.equalsIgnoreCase("Activity 2"))
Intent intent = new Intent(this, Activity2.class);
startActivity(intent);
//...and so on
【讨论】:
以上是关于如何使 SearchList 在 Android 中按字符串工作?的主要内容,如果未能解决你的问题,请参考以下文章