android ListView动态onitemclick

时间:2013-01-23 06:09:52

标签: android android-listview

我在gridview的适配器内动态创建ListView。所以会发生什么,gridview在其单元格中包含listviews。在gridview适配器内部,我使用自己的适配器创建listview。这工作正常,但是,我需要为listView编写一个onitemclick监听器来访问其每个项目的位置。目前我在创建listview后立即在gridview适配器内部编写onitemclick侦听器,但是我不确定如何访问listview项目。

请帮忙。

这是我在gridview适配器内创建listview的代码(getview方法 - 我删除了其他不相关的代码):

 public View getView(int position,View convertView, ViewGroup parent)
 {
     ListView list;

        if (convertView == null)
        {  

              //if it's not recycled, initialize some attributes.  
              list = new ListView(mContext);
              list.setVerticalScrollBarEnabled(false);
              list.setLayoutParams(new GridView.LayoutParams(150, 550));
              list.setPadding(2,2,2,2);
              list.setAdapter(new Adapter_ListView_GridView_Calendar(mContext, dagtyeVanhaarkappers.get(position-hairdresserids.size()), gebookdeurUser.get(position-hairdresserids.size()), tekening.get(position-hairdresserids.size())));
        }  
        else
        {
         list = (ListView) convertView;
        }  

        list.setCacheColorHint(0);
        list.setId(position-hairdresserids.size());

        list.setOnTouchListener(new View.OnTouchListener()
              {

                  public boolean onTouch(View v, MotionEvent event)
                  {   
                // Disallow the touch request for parent scroll on touch of child view
                      v.getParent().requestDisallowInterceptTouchEvent(true);
                      return false;
                  }
              });

       final int itemp = position;
       final int dayId = parent.getChildCount();

       list.setOnItemClickListener( new OnItemClickListener()
       {


        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
             {
               // custom dialog
        final Dialog dialog = new Dialog(mContext);
        dialog.setContentView(R.layout.dialog_make_booking);

                    //NOT SURE HOW TO ACCESS LIST ITEM POSITION FROM HERE ???

        dialog.show();
             }
       });

        return list;
     }

编辑:

这是我现在尝试的但是我一直得到0 ...

 list.setOnItemClickListener( new OnItemClickListener()
       {


             public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
             {
               // custom dialog
        final Dialog dialog = new Dialog(mContext);
        dialog.setContentView(R.layout.dialog_make_booking);

                    //NOT SURE HOW TO ACCESS LIST ITEM POSITION FROM HERE ???

                    long l = list.getAdapter().getItemId(arg2); //THIS VALUE STAYS 0
                    String s = (String) String.valueOf(l);
        dialog.setTitle(s);

        dialog.show();
             }
       });

2 个答案:

答案 0 :(得分:1)

制作当前类的listview成员,arg2是被点击的listitem的位置。

答案 1 :(得分:1)

onItemClick方法中是否无法识别itemp?

您是否尝试过将位置变量设为最终并访问它?

顺便说一句,int arg2本身应该是位置