Android programming - onitemclicklistener for multiple listviews doesn't work

前端 未结 3 1762
礼貌的吻别
礼貌的吻别 2021-01-26 00:38

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 probl

相关标签:
3条回答
  • 2021-01-26 01:20

    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.

    0 讨论(0)
  • 2021-01-26 01:29

    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

    0 讨论(0)
  • 2021-01-26 01:35

    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);
            }
    
        }
    
    0 讨论(0)
提交回复
热议问题