AlexanderZC AlexanderZC - 1 month ago 5x
Android Question

ListActivity: Add reaction when pressing on List Item

I'm just starting out with java/android studio and wrote the following ListActivity:

public class TransmitChapterSel extends ListActivity {

protected void onCreate(Bundle savedInstanceState) {

String[] chapters = {"Chapter 1", "Chapter 2", "Chapter 3"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getListView().getContext(), android.R.layout.simple_list_item_1, chapters);


But I don't know how i can produce a reaction when I click/touch/hold one of the list items.

What I want to produce is something like this:


If(ActionEvent e == Chapter1Pressed){
//do stuff



while(ActionEvent e == Chapter1HeldDown){
//do stuff

Ben Ben

If you only need to know when an item is clicked you can just use an onItemClickListener with your list view.

getListView().setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) 
   //Do stuff

If you need to know when it's held down you're going to have to create your own list adapter, and set a touch listener on the row. This is a pretty good tutorial on creating list adapters.