0

I have to a make the list view items clickable i.e. start an activity. I am using the following code. I have no clue on how to proceed.

package org.example.androidsdk.demo;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class MainActivity extends ListActivity {

    String [] mTestArray;

    /** Called when the activity is first created. */
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        // Create an ArrayAdapter that will contain all list items
        ArrayAdapter<String> adapter;

        mTestArray = getResources().getStringArray(R.array.myArray);

        /*
         * Assign the name array to that adapter and also choose a simple layout
         * for the list items
         */
        adapter = new ArrayAdapter<String>(
            this,
            android.R.layout.simple_list_item_1,
            mTestArray);

        // Assign the adapter to this ListActivity
        setListAdapter(adapter);
    }
}
Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
yogmis
  • 21
  • 4
  • http://syedasaraahmed.wordpress.com/2013/02/08/make-a-custom-listview-row-with-clickable-buttons-in-it-selectable-using-a-custom-cursoradapter/ view this link – Abhishek Chaubey Oct 31 '14 at 06:38
  • [http://www.vogella.com/tutorials/AndroidListView/article.html](http://www.vogella.com/tutorials/AndroidListView/article.html) – M D Oct 31 '14 at 06:39
  • I tell you please go through http://stackoverflow.com/help/how-to-ask coz if you google a bit before asking you might have got ur answer http://stackoverflow.com/questions/17851687/how-to-handle-the-click-event-in-listview-in-android & http://stackoverflow.com/questions/2468100/android-listview-click-howto http://stackoverflow.com/questions/6188873/event-click-on-listview-extends-listactivity & many – MOSO Oct 31 '14 at 06:41

5 Answers5

1

Just Override onListItemClick in your Activity

 @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
       String item = (String) getListAdapter().getItem(position);
       Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();
   }
M D
  • 47,665
  • 9
  • 93
  • 114
1

go to this Link it and learn how to deal with ListView http://wiresareobsolete.com/2011/08/clickable-zones-in-listview-items/

Abhishek Chaubey
  • 2,960
  • 1
  • 17
  • 24
0

You need to implement OnItemClickListener

And call getListView().setOnItemClickListener(this)

Finally, do what you want in overrided onItemClick method.

Blaze Tama
  • 10,828
  • 13
  • 69
  • 129
0

try below code :-

listView.setOnItemClickListener(new OnItemClickListener() {
  @Override
  public void onItemClick(AdapterView<?> parent, View view,
    int position, long id) {
    Toast.makeText(getApplicationContext(),
      "Click ListItem Number " + position, Toast.LENGTH_LONG)
      .show();
  }
}); 

Read below link for more information :-

http://www.vogella.com/tutorials/AndroidListView/article.html

http://androidexample.com/Create_A_Simple_Listview_-_Android_Example/index.php?view=article_discription&aid=65&aaid=90

duggu
  • 37,851
  • 12
  • 116
  • 113
0

Override onListItemClick and write your Activity open code in it.

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
      Intent i = new Intent(this,MyClass.class);
      startActivity(i);
   }
Amy
  • 4,034
  • 1
  • 20
  • 34