Android multiple list columns / grid with adapter and OnItemClickListener?

I have a simple grid with rows of elements and option columns for each element. Of course, I can manually associate OnClick actions with cell elements through specific resource identifiers:

 views.setOnClickPendingIntent(R.id.row1column1, launchA);
 views.setOnClickPendingIntent(R.id.row1column2, launchB);
    // ...
 views.setOnClickPendingIntent(R.id.row2column1, launchC);
    // ...

I would prefer to use ListView or GridView and adapter. Is there a way to get the cell id from one of them? I was hoping the ListView setOnItemClickListener () would call me with the view view identifier in the 'position' line:

onItemClick(AdapterView<?> parent, View view, int position, long id) { ... }

but don't go. Maybe a different approach?

Thank.

Update:

I started with this:

contactsView.setAdapter(new ContactAdapter(this.getApplicationContext(), R.layout.contact, contacts));
contactsLV.setOnItemClickListener(new OnItemClickListener() { ... });

I assumed that I could not enter my adapter and set the child view of OnClickListeners. Bad guess. When I returned to this, I tried:

Button x  = (Button)newView.findViewById(R.id.contact_x);
Button y = (Button)newView.findViewById(R.id.contact_y);
x.setOnClickListener(new OnClickListener() { ... });
y.setOnClickListener(new OnClickListener() { ... });
...

Voila!

+3

Source: https://habr.com/ru/post/1750764/


All Articles