0

I've created a listview extending Activity (only). I want to go to the next layout when I press the contents in listview. What can I do for this?

Taryn
  • 242,637
  • 56
  • 362
  • 405

5 Answers5

1

When you create the view for each row in your ListAdapter. You can register a OnClickListener that will be called when the user clicks on the row.

 View view = inflater.inflate(R.layout.favorite_row, null);
 view.setOnClickListener( new View.OnClickListener() {
     Override
     public void onClick(View view) {
         beverageSelected( ((FavoriteBeverageView)view.getTag()).getFavoriteBeverage() );
     }
 });
 view.setTag( new FavoriteBeverageView( view ) );

Using setTag and setId can help you find the object in your list that the user selected. Personally I think it's easiest to use setTag() adding a special object that contains the UI elements within your List row (for example titleTextView, subtitleTextView, image, etc), and add a pointer to the backing object in that special object.

In the example above the FavoriteBeverageView is that special object, and within him there is a FavoriteBeverage object that is the data that backs that list. So in the OnClickListener can easily get the FavoriteBeverage back by just doing a ((FavoriteBeverageView)view.getTag()).getFavoriteBeverage().

chubbsondubs
  • 37,646
  • 24
  • 106
  • 138
1

Android ListView and ListActivity - Tutorial can help you to how to use the list with onitemclick.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pattabi Raman
  • 5,814
  • 10
  • 39
  • 60
0

You use the onItemClick() for that.

Manuel
  • 10,153
  • 5
  • 41
  • 60
0

You will want to use OnClickListener

public OnClickListener myClickListener = new OnClickListener() { 
public void onClick(View v) {                 
 //code here that specifies what layout to go to.
   }

EDIT: I found this SO question that may contain some more information for you.

Also, if you would like to learn about intents and loading new views you can take a look at this tutorial

Community
  • 1
  • 1
sealz
  • 5,348
  • 5
  • 40
  • 70
0

See Stack Overflow question ListView without ListActivity. There's some example code from my application that works well.

Community
  • 1
  • 1
Rob
  • 2,779
  • 5
  • 23
  • 34