简体   繁体   English

如何在listView上设置项目的绝对位置?

[英]How can i set absolute positions to items on a listView?

I created some Activities on my app,so i created a listView with the name of those activities , so i can click on them and open the activity that i want (with setOnClickListener method and switch.) and ai created a searhView, so i can filter them by its name (with setOnQueryTextListener). 我在应用程序上创建了一些活动,因此我创建了带有这些活动名称的listView,因此我可以单击它们并打开想要的活动(使用setOnClickListener方法并进行切换。),并且ai创建了searhView,因此我可以按名称过滤(使用setOnQueryTextListener)。

Let's use 3 names as examples: Alfred,Beth,Bill 让我们以3个名称为例:Alfred,Beth,Bill

In the "normal" listView Alfred is 0, Beth is 1, Bill is 2. So i made this: 在“普通”列表视图中,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;

When i search in the searchView for "B" just Beth and Bill appears, and that is right, but now Beth is case 0, and Bill is case 1, so it will not open the activity that i want. 当我在searchView中搜索“ B”时,仅Beth和Bill出现,这是正确的,但是现在Beth是情况0,而Bill是情况1,因此它不会打开我想要的活动。

How can i set absolute values to the itens? 如何为itens设置绝对值? like Alfred is always 0, Beth always 1 and Bill awalys 2? 像阿尔弗雷德(Alfred)总是0,贝丝(Beth)总是1,比尔(Bill)唤醒2?

Here is my entire code. 这是我的整个代码。

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;
            }
        });





    }

}

sorry for my english 对不起我的英语不好

Modify the listener as this: 修改侦听器,如下所示:

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;
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM