Custom view with button in ArrayAdapter


I have defined a custom view (xml) for my ArrayAdapter for a ListView, which adds a Button. I'd like to create an OnClickListener for this Button per row, but without creating a custom adapter. Is that possible, or does Android force me to create a custom ArrayAdapter for my ListView?

Here's a snippet of what I'm doing:

<blockquote> glAdapter = new ArrayAdapter<Group>(getActivity(), R.layout.fragment_grouprow, R.id.groupRowText, ListOfGroups); ListView groupListView = (ListView)mainView.findViewById(R.id.listViewGroupMain); groupListView.setAdapter(glAdapter); groupListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // Call function } }); </blockquote>

Now I'd like to add an OnClickListener for the Button in my custom view.


You have to implement you own Adapter and in getView() method set OnClickListener on your button. Here kick off example:

public class CustomAdapter extends ArrayAdapter<Integer> { private ListView listView; public CustomAdapter(Context context, int textViewResourceId, Integer[] objects, ListView listView) { super(context, textViewResourceId, objects); this.listView = listView; } static class ViewHolder { TextView text; Button btn; } @Override public View getView(final int position, View convertView, ViewGroup parent) { Integer color = getItem(position); View rowView = convertView; if (rowView == null) { LayoutInflater inflater = ((Activity) getContext()).getLayoutInflater(); rowView = inflater.inflate(R.layout.list_view_row, parent, false); ViewHolder h = new ViewHolder(); h.text = (TextView) rowView.findViewById(R.id.item_text); h.btn = rowView.findViewById(R.id.btn); rowView.setTag(h); } ViewHolder h = (ViewHolder) rowView.getTag(); h.text.setText(color); h.indicator.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // DO what you want to recieve on btn click there. } }); return rowView; } }

ViewHolder here for optimization. You can read about this <a href="http://developer.android.com/training/improving-layouts/smooth-scrolling.html" rel="nofollow">there</a>.


  • Eclipse Juno/Android broken, the debug is wrong and gen folder not created (R error)
  • If OnItemClickListener is an interface in Android, how can we create an object based on it?
  • Android setOnItemClickListener
  • Setting spinner text color programmatically lags, is slow, is wrong color for split second
  • Open new Activity after click item in navigation drawer menu
  • ContextCompat.getcolor() going to null object reference
  • Android - dlopen failed: file offset for the library
  • ListView repeating the data on load more
  • Force Down error in MapView
  • Android RecyclerView Blank Space
  • OnItemSelectedListener for Spinner that was added programmatically does not trigger
  • Remove item from the listview in Android
  • How to associate .mp3 files in Android Studio
  • ASP.NET RegularExpressionValidator, validate on a non-match?
  • How to override jQuery promise callback
  • Generating anchors with PyYAML.dump()?
  • Replace Fragment with another on back button
  • Swift Initialization Rule Confusion
  • Change Checkbox value without raising event
  • LiveData is abstract android
  • CSS - Cannot get one spanned style to override another inherited style and align left
  • Authentication failed with Azure Active Directory in Windows Phone
  • How to get listview position?
  • Regarding starting the threads on a condition
  • Center align outputs in ipython notebook
  • Android Activity.onWindowFocusChanged doesn't get called from within TabHost
  • Zoom in and out of jPanel
  • GridView breaks while scrolling
  • NHibernate Validation Localization with S#arp Architecture
  • how to do an event when i swipe from fragment to the other
  • Update CALayer sublayers immediately
  • JFileChooser in front of fullscreen Swing application
  • Validaiting emails with Net.Mail MailAddress
  • sending/ receiving email in Java
  • Cannot Parse HTML Data Using Android / JSOUP
  • JTable with a ScrollPane misbehaving
  • How to stop GridView from loading again when I press back button?
  • unknown Exception android
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?