Edit: This is different than the other question because it asks how to call a basic intent.
I am a beginner at coding and I have a Listview and several items
I want each of these list views to call a different method.
How can this be achieved? I have tried putting the method under the layout of the list item, however this causes all items to call the same method.
The method being called will launch a separate activity containing information. How do I make it so that when each of the items are clicked a different method is called? Thanks for helping out a noob!
Code of list view: activity_main.xml
<ListView
android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
Mainactivity.java: containing list view and method to be called
public class MainActivity extends ListActivity {
String[] itemname ={
"Nucleus",
"Cytoplasm",
"Mitochondria",
"Golgi Apparatus",
"Ribosomes",
"Vacuole",
"Chloroplast",
"Central Vacuole",
"Cell Membrane",
"Cell Wall"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.setListAdapter(new ArrayAdapter<String>(
this, R.layout.mylist,
R.id.Itemname,itemname));
}
public void explainCellMembrane(View view) {
startActivity(new Intent(MainActivity.this, MembraneActivity.class));
}
My List item layout: mylist.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:src="@drawable/sample_image" />
<TextView
android:id="@+id/Itemname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:paddingTop="5dp"
android:onClick="explainCellMembrane"/>
</LinearLayout>