【问题标题】:How can i set absolute positions to items on a listView?如何为 listView 上的项目设置绝对位置?
【发布时间】: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


【解决方案1】:

修改监听器如下:

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

【讨论】:

    猜你喜欢
    • 2019-05-09
    • 2022-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多