Android编程-用于多个列表视图的onitemclicklistener不起作用 [英] Android programming - onitemclicklistener for multiple listviews doesn't work

查看:129
本文介绍了Android编程-用于多个列表视图的onitemclicklistener不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的活动中,我创建了七个列表视图,并且正在使用viewpager在同一活动中在它们之间滑动.然后,我有一个填充每个列表视图的sqlite数据库.我的问题是onitemclicklistener无法正常工作,没有错误,代码执行正常,但列表项单击没有任何反应.我通过添加烤面包显示消息等对它进行了测试,但列表项单击没有任何反应.

In my activity i have created seven listviews and am using viewpager to swipe between them in the same activity. I then have a sqlite database populating each listview. My problem is the onitemclicklistener is not working, there are no errors and the code executes fine but nothing happens on list item clicks. I tested it out by adding toast display messages etc but nothing happens on list item clicks.

我怀疑问题是我没有像常规方法那样从xml布局中获取列表视图,所以itemclicklistener方法略有不同,但是我不知道以这种方式利用列表视图时要使用哪种方法,并且没有其他操作系统线程可以使用我见过以这种方式创建listviews. 如果有人可以请大家说明一下,我将不胜感激.提前致谢.

I suspect the problem is i have not gotten the listviews from xml layouts like the conventional method so the itemclicklistener method is slightly different, however i don't know what method to use when utilising listviews this way and no other OS threads that i've seen creates listviews this way. If anyone could please shed some light i'd be very grateful. Thanks in advanced.

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        mContext = this;
        setContentView(R.layout.activity_schedule);

        ListView listview1 = new ListView(mContext);
        ListView listview2 = new ListView(mContext);
        ListView listview3 = new ListView(mContext);
        ListView listview4 = new ListView(mContext);
        ListView listview5 = new ListView(mContext);
        ListView listview6 = new ListView(mContext);
        ListView listview7 = new ListView(mContext);


        Vector<View> pages = new Vector<View>();

        pages.add(listview1);
        pages.add(listview2);
        pages.add(listview3);
        pages.add(listview4);
        pages.add(listview5);
        pages.add(listview6);
        pages.add(listview7);

        ViewPager vp = (ViewPager) findViewById(R.id.viewpager);
        PageAdapter adapter = new PageAdapter(mContext,pages);
        vp.setAdapter(adapter);


        db.open();
        scheduleAdapter = new ScheduleAdapter(ScheduleActivity.this, db.getMonday(),CursorAdapter.NO_SELECTION );
        listview1.setAdapter(scheduleAdapter);      
        db.close();

        db.open();
        scheduleAdapter = new ScheduleAdapter(ScheduleActivity.this, db.getTuesday(),CursorAdapter.NO_SELECTION );
        listview2.setAdapter(scheduleAdapter);      
        db.close();

        db.open();
        scheduleAdapter = new ScheduleAdapter(ScheduleActivity.this, db.getWednesday(),CursorAdapter.NO_SELECTION );
        listview3.setAdapter(scheduleAdapter);      
        db.close();

        db.open();
        scheduleAdapter = new ScheduleAdapter(ScheduleActivity.this, db.getThursday(),CursorAdapter.NO_SELECTION );
        listview4.setAdapter(scheduleAdapter);      
        db.close();

        db.open();
        scheduleAdapter = new ScheduleAdapter(ScheduleActivity.this, db.getFriday(),CursorAdapter.NO_SELECTION );
        listview5.setAdapter(scheduleAdapter);      
        db.close();

        db.open();
        scheduleAdapter = new ScheduleAdapter(ScheduleActivity.this, db.getSaturday(),CursorAdapter.NO_SELECTION );
        listview6.setAdapter(scheduleAdapter);      
        db.close();

        db.open();
        scheduleAdapter = new ScheduleAdapter(ScheduleActivity.this, db.getSunday(),CursorAdapter.NO_SELECTION );
        listview7.setAdapter(scheduleAdapter);      
        db.close();

         listview1.setOnItemClickListener(new ListView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

                }
        });

推荐答案

从您的描述中我了解到,启动应用程序时会显示该列表.

From your describe I understand that the lists are showed up when you start the application.

position参数,用于保存用户在(屏幕)-列表上单击的位置.

The position parameter, holds where the user clicked on the (screen) - list.

您是否尝试将其与开关盒一起使用?

Did you try to use it with switch case?

我的意思是这样:

list.setOnItemClickListener(new OnItemClickListener(){
                  public void onItemClick( AdapterView <?> parent, View view, int   
                                           position,long id){

                       switch(position){
                            case 0:
                           // write what you need here when the user clicks on the first list item
                              break;
                            case 1:
                           // write what you need here when the user clicks on the 2nd list item 
                               break;
                       }
                  }
               };

希望这对您有帮助

这篇关于Android编程-用于多个列表视图的onitemclicklistener不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆