2016-12-30 77 views
1

我在我的应用程序中创建了一些活动,所以我创建了一个listView与这些活动的名称,所以我可以点击它们并打开我想要的活动(使用setOnClickListener方法和开关。),并且创建了searhView,我可以通过它的名字来过滤它们(用setOnQueryTextListener)。我如何设置绝对位置到listView上的项目?

让我们使用3名为例:阿尔弗雷德,贝斯,比尔

在 “正常” 的ListView阿尔弗雷德是0,贝丝是1,比尔是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的情况是0,而Bill是情况1,所以它不会打开我想要的活动。

我该如何设置绝对值到itens?像阿尔弗雷德永远是0,贝丝总是1,比尔是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; 
      } 
     }); 





    } 

} 

对不起,我的英语

+1

你为什么不改变从'position'到'itemValue'交换机?即:switch(itemValue){case:“Beth”:// goToBeth; break;}' –

+1

首先,您最好创建一个单独的活动并将名称传递给它。其次,如果你仍然坚持单独的活动,而不是检查位置检查选择的名称 – akash93

+0

@AhmedAbidi正是我想要的,谢谢! –

回答

3

修改监听器,因为这:

v.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      // ListView Clicked item value 
      String itemValue = (String) lv.getItemAtPosition(position); 

      // Show Alert 
      Toast.makeText(getApplicationContext(), "Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG).show(); 

      switch(itemValue) 
      { 
       case "alfred": Intent alfred = new Intent(telaPesquisa.this, alfred.class); 
        startActivity(alfred); 
        break; 

       case "beth": Intent beth = new Intent(telaPesquisa.this, beth.class); 
        startActivity(beth); 
        break; 

       case "bill": Intent bill = new Intent(telaPesquisa.this, bill.class); 
        startActivity(bill); 
        break; 
     } 
    }); 
+0

正是我想要的,谢谢! –