0

Usually, I assign button click handler as follows-

//activity_main.xml
<Button android:onClick="DoWork" />

//MainActivity.java
public void DoWork(View view){}

Now, I have got a ListView which click event handler is as the following-

protected void onCreate(Bundle savedInstanceState) {
    final ListView listView = (ListView) findViewById(R.id.CategoryListView);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        public void onItemClick(AdapterView<?> arg0, View v, int position, long id)
        {

        }
    });
}

If I would like to do <ListView android:onClick="Something"/> , how should I define the method?

Sorry if this is a foolish question. Thank you.

s.k.paul
  • 7,099
  • 28
  • 93
  • 168
  • Do you want to handle an item click or the whole listview click? – Nikola Jun 09 '16 at 11:12
  • I think it is impossible. You can set listener to your list items, not to your `listView` – SripadRaj Jun 09 '16 at 11:13
  • Its not possible for Listview.. but you can set onClick for listview itme – Mehta Jun 09 '16 at 11:14
  • @Mehta, Well, then suggest for item. – s.k.paul Jun 09 '16 at 11:16
  • Item means listview child.. and handle click event in your adapter – Mehta Jun 09 '16 at 11:17
  • @Nikola, Just an item click, not the entire listview. – s.k.paul Jun 09 '16 at 11:18
  • I think you code the onclick in the view that is displayed/viewed by the listview. i.e. within the layout that is called by the listview's adapter. (only done this with custom adapters). – MikeT Jun 09 '16 at 11:25
  • e.g. I have the following ``. This is in shopping_list_entry, which is invoked by `public View newView(Context context, Cursor cursor, ViewGroup parent) { return LayoutInflater.from(context).inflate(R.layout.shopping_list_entry, parent, false);` This is within the adapter used by the Listview. The method (sledone) is within the activity that invokes Listview. – MikeT Jun 09 '16 at 11:35
  • Oops sorry, that was for a view within the item. It's easier, you just code onclick="yourmethod" and include the method in the activity. If you code the method first then Studio will display that method when you input onclick (at least it does for me). – MikeT Jun 09 '16 at 11:45
  • Possible duplicate of [Android - ListView click HOWTO?](http://stackoverflow.com/questions/2468100/android-listview-click-howto) – Nikola Jun 09 '16 at 11:48

0 Answers0