ListItem not displayed in second activity in android -
i have made simple listview demo in android. want when item list selected should displayed on next activity,i have tried following code:
act1.java
package com.example.listview; import java.util.arraylist; import android.os.bundle; import android.app.activity; import android.content.intent; import android.view.menu; import android.view.view; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.arrayadapter; import android.widget.listview; import android.widget.simpleadapter; public class mainactivity extends activity { listview ls; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); ls=(listview)findviewbyid(r.id.lv); string[] s = {"ahmedabad","baroda","chndigarh","delhi","jharkhand","hariyana","ilahabad","jammu","kashmir","ludhina","mysore","nagpur", "odisha","puri","raipur","surat","triveni","udaipur","varanasi"}; //final arraylist<string> list = new arraylist<string>(); ls.setadapter(new arrayadapter(this, android.r.layout.simple_list_item_1,s)); ls.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> arg0, view arg1, int arg2, long arg3) { // todo auto-generated method stub intent intent = new intent(mainactivity.this,activity2.class); string city=(string) ls.getselecteditem(); bundle b =new bundle(); intent.putextra("city", city); startactivity(intent); } }); } }
act2.java
package com.example.listview; import android.os.bundle; import android.app.activity; import android.content.intent; import android.view.menu; import android.widget.textview; public class activity2 extends activity { textview tv; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_activity2); tv=(textview)findviewbyid(r.id.textview1); intent =getintent(); bundle b =getintent().getextras(); string city =b.getstring("city"); tv.settext(city); } }
please help... thanks.
instead of this
string city=(string) ls.getselecteditem();
use this
string city=arg0.getitematposition(position);
Comments
Post a Comment