我在gridview的适配器内动态创建一个ListView。所以发生的事情是,gridview 在其单元格中包含列表视图。在 gridview 适配器内部,我使用自己的适配器创建列表视图。这很好用,但是,我需要为 listView 编写一个 onitemclick 侦听器来访问其每个项目的位置。目前,我在创建列表视图后立即在 gridview 适配器内编写 onitemclick 侦听器,但是我不确定如何访问列表视图项目。
请帮忙。
这是我在 gridview 适配器内部创建列表视图的代码(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();
}
});