java - Spinner with list view -
i ask, i'm working spinner. i've list view , spinner. in spinner declare "month", "june","july","august","sept","oct","nov","dec"
in list view, originally, empty. when click on "august" , show data of august(august.java) in list view, may know there way it?
because right , i'm doing via class.. meaning, when onclick aug, open new intent. wanted merge 1 class, when click "august", change in list view , not open new intent. there way it?
this code
string [] months = { "june", "july", "august", "septemeber", "november", "december",
part of date.java
//spinnerview s1 = (spinner) findviewbyid(r.id.spinner1); arrayadapter<string> adapter = new arrayadapter<string>(this,android.r.layout.simple_spinner_item, months); s1.setadapter(adapter); s1.setonitemselectedlistener(new onitemselectedlistener() { public void onitemselected(adapterview<?> arg0,view arg1, int arg2, long arg3) { int index = s1.getselecteditemposition(); //toast.maketext(getbasecontext(), "you have seleted item :" + months[index] , toast.length_short).show(); } public void onnothingselected(adapterview<?>arg0) {} }); if ( s1.equals("august")) { startactivity(new intent(date.this,mainactivity.class)); } else{ startactivity(new intent(date.this,august .class)); }
data code i'm getting. august.java
public class august extends listactivity { string url = "http://api.eventful.com/rest/events/search?app_key=42t54cx7rbrdfczc&location=singapore&page_size=20&date=august"; // xml node keys static final string key_event = "event"; // parent node static final string key_title = "title"; static final string key_url = "url"; static final string key_desc = "description"; static final string key_start_time = "start_time"; static final string key_stop_time = "stop_time"; static final string key_time = "combine_time"; static final string key_venue_name = "venue_name"; static final string key_country_name = "country_name"; static final string key_venue_address = "venue_address"; static final string key_venue = "venue"; static final string key_latitude = "latitude"; static final string key_longitude = "longitude"; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.date); final arraylist<hashmap<string, string>> menuitems = new arraylist<hashmap<string, string>>(); xmlparser parser = new xmlparser(); string xml = parser.getxmlfromurl(url); // getting xml document doc = parser.getdomelement(xml); // getting dom element nodelist nl = doc.getelementsbytagname(key_event); // looping through item nodes <item> (int = 0; < nl.getlength(); i++) { // creating new hashmap hashmap<string, string> map = new hashmap<string, string>(); element e = (element) nl.item(i); // adding each child node hashmap key => value map.put(key_title, parser.getvalue(e, key_title)); map.put(key_url, parser.getvalue(e, key_url)); map.put(key_desc, "description: " + parser.getvalue(e, key_desc)); map.put(key_start_time, parser.getvalue(e, key_start_time)); map.put(key_stop_time, parser.getvalue(e, key_stop_time)); if (parser.getvalue(e, key_stop_time) != "") { map.put(key_time, parser.getvalue(e, key_start_time) + "-" + parser.getvalue(e, key_stop_time)); } else { map.put(key_time, parser.getvalue(e, key_start_time)); } map.put(key_venue_name, parser.getvalue(e, key_venue_name)); map.put(key_country_name, parser.getvalue(e, key_country_name)); map.put(key_venue_address, parser.getvalue(e, key_venue_address)); map.put(key_latitude, parser.getvalue(e, key_latitude)); map.put(key_longitude, parser.getvalue(e, key_longitude)); map.put(key_venue, parser.getvalue(e, key_venue_name) + ", " + parser.getvalue(e, key_venue_address)); // adding hashlist arraylist menuitems.add(map); } // adding menuitems listview listadapter adapter = new simpleadapter(this, menuitems, r.layout.list_item, new string[] { key_title, key_desc, key_country_name, key_venue, key_latitude, key_longitude, key_time, }, new int[] { r.id.title, r.id.description, r.id.countryname, r.id.venuename, r.id.lat, r.id.lng, r.id.starttime }); setlistadapter(adapter); // selecting single listview item listview lv = getlistview(); lv.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { // getting values selected listitem string title = ((textview) view.findviewbyid(r.id.title)) .gettext().tostring(); string description = ((textview) view .findviewbyid(r.id.description)).gettext().tostring(); string venue = ((textview) view.findviewbyid(r.id.venuename)) .gettext().tostring(); string lat = ((textview) view.findviewbyid(r.id.lat)).gettext() .tostring(); string lng = ((textview) view.findviewbyid(r.id.lng)).gettext() .tostring(); // starting new intent intent in = new intent(getapplicationcontext(), singlemenuitemactivity.class); in.putextra(key_title, title); in.putextra(key_desc, description); in.putextra(key_venue, venue); in.putextra(key_latitude, lat); in.putextra(key_longitude, lng); startactivity(in); } }); } }
use following code have tried working fine
import java.util.arraylist; import java.util.list; import android.app.activity; import android.os.bundle; import android.view.menu; import android.view.view; import android.widget.adapterview; import android.widget.arrayadapter; import android.widget.spinner; import android.widget.toast; import android.widget.adapterview.onitemselectedlistener; public class mainactivity extends activity implements onitemselectedlistener { private spinner sp; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); ui(); spelements(); } public void ui() { sp = (spinner) findviewbyid(r.id.mydsp); sp.setonitemselectedlistener(this); } public void spelements() { list<string> months = new arraylist<string>(); months.add("june"); months.add("july"); months.add("august"); months.add("september"); months.add("october"); months.add("november"); months.add("december"); // creating adapter spinner arrayadapter<string> dataadapter = new arrayadapter<string>(this, android.r.layout.simple_spinner_item, months); // drop down layout style - list view radio button dataadapter .setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); // attaching data adapter spinner sp.setadapter(dataadapter); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.main, menu); return true; } @override public void onitemselected(adapterview<?> main, view view, int position, long id) { string item = main.getitematposition(position).tostring(); int index = item.getselecteditemposition(); toast.maketext(main.getcontext(), "you selected month is: " + item, toast.length_long).show(); } @override public void onnothingselected(adapterview<?> arg0) { // todo auto-generated method stub } }
Comments
Post a Comment