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

时间:2016-12-30 14:44:34

标签: java android listview android-studio

我在我的应用程序上创建了一些活动,所以我创建了一个listView,其中包含这些活动的名称,因此我可以单击它们并打开我想要的活动(使用setOnClickListener方法并切换。)并且ai创建了一个searhView,所以我可以按名称过滤它们(使用setOnQueryTextListener)。

让我们使用3个名字作为例子:Alfred,Beth,Bill

在"正常" listView 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是0,而Bill是案例1,所以它不会打开我想要的活动。

如何设置绝对值?就像阿尔弗雷德总是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 个答案:

答案 0 :(得分: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;
        }
    });
相关问题