【发布时间】:2017-05-14 20:41:54
【问题描述】:
我在我的应用程序上创建了一些活动,所以我用这些活动的名称创建了一个 listView,所以我可以单击它们并打开我想要的活动(使用 setOnClickListener 方法和开关。)然后 ai 创建了一个搜索视图,所以我可以按名称过滤它们(使用 setOnQueryTextListener)。
我们以 3 个名字为例:Alfred,Beth,Bill
在“正常”列表视图中,Alfred 是 0,Beth 是 1,Bill 是 2。所以我做了这个:
switch( position )
{
case 0: Intent newActivity = new Intent(telaPesquisa.this, alfred.class);
startActivity(newActivity);
break;
case 1: Intent newActivityy = new Intent(telaPesquisa.this, beth.class);
startActivity(newActivityy);
break;
case 2: Intent newActivity2 = new Intent(telaPesquisa.this, bill.class);
startActivity(newActivity2);
break;
当我在 searchView 中搜索“B”时,只出现了 Beth 和 Bill,没错,但现在 Beth 是 case 0,Bill 是 case 1,所以它不会打开我想要的活动。
如何设置itens 的绝对值?比如 Alfred 总是 0,Beth 总是 1,Bill awalys 2?
这是我的全部代码。
public class telaPesquisa extends Activity {
ListView lv;
SearchView sv;
String[] teams={
"Alfred",
"Beth",
"Bill",
};
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tela_pesquisa);
lv=(ListView) findViewById(R.id.listView);
sv=(SearchView) findViewById(R.id.searchView);
adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,teams);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//nt firstPosition = lv.getFirstVisiblePosition()+3;
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) lv.getItemAtPosition(position);
// Show Alert
Toast.makeText(getApplicationContext(), "Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG).show();
switch( position )
{
case 0: Intent alfred = new Intent(telaPesquisa.this, alfred.class);
startActivity(alfred);
break;
case 1: Intent beth = new Intent(telaPesquisa.this, beth.class);
startActivity(beth);
break;
case 2: Intent bill = new Intent(telaPesquisa.this, bill.class);
startActivity(bill);
break;
}
});
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;
}
});
}
}
对不起我的英语
【问题讨论】:
-
你为什么不把你的开关从
position改成itemValue?即:switch(itemValue) { case : "Beth": //goToBeth; break;} -
首先,您最好创建一个活动并将名称传递给它。其次,如果您仍然坚持进行单独的活动,而不是根据所选名称检查位置检查
-
@AhmedAbidi 正是我想要的,谢谢!
标签: java android listview android-studio