SoFunction
Updated on 2025-03-02

Two ways to respond to click events of ListView internal buttons in Activity in android

Recently, someone in the communication group asked a question: How to respond to the click event of the ListView internal button in Activity, and not respond in Adapter?

For this question, my initial answer was to define a callback interface in Adapter and implement it in Activity to achieve response to click events.

After get off work, I thought about it and felt that there are two ways to implement it better: using interface callbacks and using abstract class callbacks.

It just so happened that we can review the difference between interfaces and abstract classes, so we wrote two demos:

1. Use interface callback:

Adapter class

package ; 
 
import ; 
 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
 
import ; 
 
public class ContentAdapter extends BaseAdapter implements OnClickListener { 
 
  private static final String TAG = "ContentAdapter"; 
  private List<String> mContentList; 
  private LayoutInflater mInflater; 
  private Callback mCallback; 
 
  /**
    * Custom interface for callback button click event to Activity
    * @author Ivan Xu
    * 2014-11-26
    */ 
  public interface Callback { 
    public void click(View v); 
  } 
 
  public ContentAdapter(Context context, List<String> contentList, 
      Callback callback) { 
    mContentList = contentList; 
    mInflater = (context); 
    mCallback = callback; 
  } 
 
  @Override 
  public int getCount() { 
    (TAG, "getCount"); 
    return (); 
  } 
 
  @Override 
  public Object getItem(int position) { 
    (TAG, "getItem"); 
    return (position); 
  } 
 
  @Override 
  public long getItemId(int position) { 
    (TAG, "getItemId"); 
    return position; 
  } 
 
  @Override 
  public View getView(int position, View convertView, ViewGroup parent) { 
    (TAG, "getView"); 
    ViewHolder holder = null; 
    if (convertView == null) { 
      convertView = (.list_item, null); 
      holder = new ViewHolder(); 
       = (TextView) convertView 
          .findViewById(.textView1); 
       = (Button) (.button1); 
      (holder); 
    } else { 
      holder = (ViewHolder) (); 
    } 
    ((position)); 
 
     
    (this); 
    (position); 
    return convertView; 
  } 
 
  public class ViewHolder { 
    public TextView textView; 
    public Button button; 
  } 
 
  //Respond to button click event, call the sub-definition interface, and pass in View  @Override 
  public void onClick(View v) { 
    (v); 
  } 
} 

Activity class:

package ; 
 
import ; 
import ; 
 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
 
import ; 
import ; 
import ; 
//MainActivity needs to implement a custom interfacepublic class MainActivity extends Activity implements OnItemClickListener, 
    Callback { 
 
  // Simulate the data loaded in the listview  private static final String[] CONTENTS = { "Beijing", "Shanghai", "Guangzhou", "Shenzhen", "Suzhou", 
      "Nanjing", "Wuhan", "Changsha", "Hangzhou" }; 
  private List<String> contentList; 
  private ListView mListView; 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    (savedInstanceState); 
    setContentView(.activity_main); 
 
    init(); 
  } 
 
  private void init() { 
    mListView = (ListView) findViewById(); 
    contentList = new ArrayList<String>(); 
    for (int i = 0; i < ; i++) { 
      (CONTENTS[i]); 
    } 
    // 
    (new ContentAdapter(this, contentList, this)); 
    (this); 
  } 
 
  @Override 
  public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(, menu); 
    return true; 
  } 
 
  /**
    * Respond to the click event of the item in ListView
    */ 
  @Override 
  public void onItemClick(AdapterView<?> arg0, View v, int position, long id) { 
    (this, "The item of listview was clicked!, the clicked position is -->" + position, 
        Toast.LENGTH_SHORT).show(); 
  } 
 
  /**
    * Interface method, responding to ListView button click event
    */ 
  @Override 
  public void click(View v) { 
    ( 
        , 
        "The button inside the listview was clicked!, the position is -->" + (Integer) () + ",The content is -->" 
            + ((Integer) ()), 
        Toast.LENGTH_SHORT).show(); 
  } 
} 

2. Use abstract class callbacks

Adapter class:

package ; 
 
import ; 
 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
 
import ; 
 
public class ContentAdapter extends BaseAdapter { 
 
  private static final String TAG = "ContentAdapter"; 
  private List<String> mContentList; 
  private LayoutInflater mInflater; 
  private MyClickListener mListener; 
 
  public ContentAdapter(Context context, List<String> contentList, 
      MyClickListener listener) { 
    mContentList = contentList; 
    mInflater = (context); 
    mListener = listener; 
  } 
 
  @Override 
  public int getCount() { 
    (TAG, "getCount"); 
    return (); 
  } 
 
  @Override 
  public Object getItem(int position) { 
    (TAG, "getItem"); 
    return (position); 
  } 
 
  @Override 
  public long getItemId(int position) { 
    (TAG, "getItemId"); 
    return position; 
  } 
 
  @Override 
  public View getView(int position, View convertView, ViewGroup parent) { 
    (TAG, "getView"); 
    ViewHolder holder = null; 
    if (convertView == null) { 
      convertView = (.list_item, null); 
      holder = new ViewHolder(); 
       = (TextView) convertView 
          .findViewById(.textView1); 
       = (Button) (.button1); 
      (holder); 
    } else { 
      holder = (ViewHolder) (); 
    } 
    ((position)); 
    (mListener); 
    (position); 
    return convertView; 
  } 
 
  public class ViewHolder { 
    public TextView textView; 
    public Button button; 
  } 
 
  /**
    * Abstract class for callbacks
    * @author Ivan Xu
    * 2014-11-26
    */ 
  public static abstract class MyClickListener implements OnClickListener { 
    /**
      * The onClick method of the base class
      */ 
    @Override 
    public void onClick(View v) { 
      myOnClick((Integer) (), v); 
    } 
    public abstract void myOnClick(int position, View v); 
  } 
} 

Activity class:

package ; 
 
import ; 
import ; 
 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
 
import ; 
import ; 
import ; 
 
public class MainActivity extends Activity implements OnItemClickListener { 
 
  // Simulate the data loaded in the listview  private static final String[] CONTENTS = { "Beijing", "Shanghai", "Guangzhou", "Shenzhen", "Suzhou", 
      "Nanjing", "Wuhan", "Changsha", "Hangzhou" }; 
  private List<String> contentList; 
  private ListView mListView; 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    (savedInstanceState); 
    setContentView(.activity_main); 
 
    init(); 
  } 
 
  private void init() { 
    mListView = (ListView) findViewById(); 
    contentList = new ArrayList<String>(); 
    for (int i = 0; i < ; i++) { 
      (CONTENTS[i]); 
    } 
    //Instantiate the ContentAdapter class and pass in the implementation class    (new ContentAdapter(this, contentList, mListener)); 
     
    (this); 
  } 
 
  @Override 
  public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(, menu); 
    return true; 
  } 
 
  //Respond to item click event  @Override 
  public void onItemClick(AdapterView<?> arg0, View v, int position, long id) { 
    (this, "The item of listview was clicked!, the clicked position is -->" + position, 
        Toast.LENGTH_SHORT).show(); 
  } 
 
  /**
    * Implementation class, respond to button click event
    */ 
  private MyClickListener mListener = new MyClickListener() { 
    @Override 
    public void myOnClick(int position, View v) { 
      ( 
          , 
          "The button inside the listview was clicked!, the position is -->" + position + ",The content is -->" 
              + (position), Toast.LENGTH_SHORT) 
          .show(); 
    } 
  }; 
} 

The difference between the two methods is that when an abstract class is implemented in the Activity, it can only define a member variable to implement it, and cannot be directly implemented by the Activity, because Java does not support multiple inheritance. The interface can be implemented directly by Activity or by its member variables.

Original link: /u011895534/article/details/50439547

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.