Skyler
Skyler

Reputation: 47

How to add custom click events to dynamic list view?

I have a list view that automatically generates its content from an sql database. I would like specific events to happen to each member of the listview when clicked, based on their content. How do I go about doing this?

Upvotes: 0

Views: 92

Answers (1)

Jared Rummler
Jared Rummler

Reputation: 38121

I would like specific events to happen to each member of the listview when clicked

Set an AdapterView.OnItemClickListener on your ListView.

based on their content

Get the item at the current position and handle the event:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

  @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Object item = parent.getAdapter().getItem(position);
    if ( /* your condition here */ ) {

    } else if ( /* something else */ ) {

    }
  }
});

Upvotes: 2

Related Questions