Reputation: 2284
I am creating a ListView dynamically inside of the adapter of a gridview. So what happens is, the gridview contains listviews in its cells. Inside of the gridview adapter, I create the listview with its own adapter. This works fine, however, I need to write a onitemclick listener for the listView to access the position of each of its items. Currently I am writing the onitemclick listener inside of the gridview adapter right after I have created the listview, however I am not sure how to gain access to the listview items.
Please help.
Here is the code where I create the listview, inside of the gridview adapter (getview method - I deleted the other irrelevant code):
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;
}
EDIT:
This is what I tried now but I keep getting a value of 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();
}
});
Upvotes: 1
Views: 955
Reputation: 2986
Is the itemp not recognized inside the onItemClick method?
Have you tried making the position variable final and access it?
Btw, int arg2 should be the position in itself
Upvotes: 1
Reputation: 2122
Make the listview member of the current class and arg2 is the position of the listitem that was clicked.
Upvotes: 1