Android programming - onitemclicklistener for multiple listviews doesn't work

那年仲夏 提交于 2020-01-11 13:26:08

问题


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.

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) { 

                }
        });

回答1:


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

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

Did you try to use it with switch case?

I mean like this:

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

hope this will help you




回答2:


I'm not quite sure why the itemClicklistener isn't being called when you press something in listview1, but I don't think that's the biggest problem.

You're off on a terrible start with adding 7 listviews in 1 activity, and with opening and closing your database 7 times straight after each other.

I'd suggest you start out with the app Android UI Patterns: https://play.google.com/store/apps/details?id=com.groidify.uipatterns

There you will find a page full of viewpagers and tabs examples. I'd suggest you take the Jake Wharton one.

You want to be doing this with Fragments.




回答3:


In your ListFragment you should extends ListFragment then use onListItemClick, like this:

public class ArrayListFragment extends ListFragment {

@Override                               
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            setListAdapter(new ArrayAdapter<String>(getActivity(),
                    android.R.layout.simple_list_item_1, Listnames.TITLES));
        }

        @Override
        public void onListItemClick(ListView l, View v, int position, long id) {
            Log.i("FragmentList2", "Item clicked: " + id);

            String item = (String) getListAdapter().getItem(position);

            Intent intent = new Intent(getActivity(), SearchableActivity.class);
            intent.putExtra("item", item);
            Toast.makeText(getActivity(), item, Toast.LENGTH_LONG).show();
//          startActivity(intent);
        }

    }


来源:https://stackoverflow.com/questions/14308192/android-programming-onitemclicklistener-for-multiple-listviews-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!