0

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>

2 Answers2

0
 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                        }
                    });
Suraj Vaishnav
  • 7,777
  • 4
  • 43
  • 46
0
listView.setOnItemClickListener(new OnItemClickListener() {
 @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Object o = prestListView.getItemAtPosition(position);
        //....
    }
});

Reference

Frank Odoom
  • 1,545
  • 18
  • 19
  • Hello I am a complete beginner, where do i put the method that I want to call? and what do the int position, long id words mean?? – Shun Long TSANG Jan 03 '18 at 06:00
  • The position of the view in the adapter and The row id of the item that was clicked – Frank Odoom Jan 03 '18 at 06:06
  • could you perhaps help me put it in my code? Im sorry I dont understand because I am a complete beginner. Maybe you could be so kind and post another answer containing the code put into my code? Thankyou so much if you could do that – Shun Long TSANG Jan 03 '18 at 06:10