Go to the next layout when pressing the contents in a listview in Android
I've created a listview extending Activity (only). I want to go to the next layout when I pr开发者_JAVA技巧ess the contents in listview. What can I do for this?
Android ListView and ListActivity - Tutorial can help you to how to use the list with onitemclick.
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().
You use the onItemClick() for that.
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
See Stack Overflow question ListView without ListActivity. There's some example code from my application that works well.
精彩评论