I currently have an Android ListView class that displays a list of about 20 topics (Strings). I need to be able to click each button on the list and have that button open up a view specific to that topic.
For example, if this was a recipe list, then the layout for all of the recipe views could be the same, but when the user clicks a specific recipe from the list, then the program must load that recipe into the common layout and bring the user to that view.
I have the OnItemClickListener working I think, but I’m not sure how to implement the rest.
Will I need a new activity and layout for each recipe? Is there an easier way to implement this without making dozens of identical layout and activity files?
Also, how will I populate the view with the recipe?
Thanks much appreciated for any helpful thoughts!
— Some relevant code: The Listview Activity Code
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, studiesList);
// Set the ArrayAdapter as the ListView's adapter.
mainListView.setAdapter( listAdapter );
mainListView.setClickable(true);
mainListView.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> a, View view, int position, long id) {
switch( position )
{
case 0: Intent intent = new Intent(StudyActivity.this, pos.class);
startActivity(intent);
break;
The SimpleRow.xml File: (The buttons for the list)
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</Button>
What you need to do is make a serializable recipe class with some attributes, and then make new objects of that class for each of the 20 recipes.
I assume you’d have something like
}
Then make an array list of these objects
and use that arraylist in your list adapter.
Then in your onItemClickListener you’l need something like
and in your recipe display class just recieve the intent and use the object to populate your activity fields.