Sunday, 15 January 2012

java - How to get listview item string? -


How to get an item list How do I click in the list-view?

Here is my code:

  Public Class Main Class Activity {ArrayList & lt; String & gt; ListItems = New ArrayList & lt; String & gt; (); ArrayAdapter & lt; String & gt; Adapter; See list view list; @ Override protected void saved create (bundled Instansstet) {Suprknkret (saved Instensstet); SetContentView (R.layout.main_layout); Adapter = new array adapter & lt; String & gt; (This, Android .R.Lite.Simple_L_Lightime, LISTEATIM); ListView = (ListView) findViewById (R.id.listView1); ListView.setAdapter (adapter); ListItems.add ("item 1"); ListItems.add ("Item 2"); ListItems.add ("Item 3"); Adapter.notifyDataSetChanged (); ListView.setOnClickListener (New View. OnClickListener) {@Override Public Zero onClick (see arg0) {// TODO Automatic Created Method Stub Toast.makeText (getApplicationContext (), listView.getItemAtPosition (0) .toString (), Toast LENGTH_LONG) .show ();}}); }}   

But this force closes

Change:

  listView.setOnClickListener (New View. OnClickListener) {@Override Public Zero onClick (see arg0) {// TODO Automatic Created Method Stub Toast.makeText (getApplicationContext (), ListView.getItemAtPosition (0) .toString (), toast.langtha_long) show ();}});   

:

  listView.setOnItemClickListener (New OnItemClickListener () {@Override Public Zero onItemClick (AdapterView & lt; & gt; arg0, see ARG1, integer ARG2, long arg3) {toast.makeText (getApplicationContext (), listView.getItemAtPosition (aRG2) .toString (), Toast.LENGTH_LONG) .show ();}});    

No comments:

Post a Comment