java - Trying to select an item from a listView that is populated from an xml string array -
this question has answer here:
- how compare strings in java? 23 answers
so had string array in mainactivity.java populated listview , able click on item in listview , open new activity. i've replaced string array xml string array resource populate listview. cannot select items listview. hope explained well. thank help!
package com.example.zach.listview; import android.content.intent; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; import android.widget.adapterview; import android.widget.arrayadapter; import android.widget.listadapter; import android.widget.listview; import android.widget.textview; import android.widget.toast; public class mainactivity extends appcompatactivity { //public static string[] routes = {"information","crag map","main wall","1. shark bait - 5.9", "2. rain check - 5.8", "3. rain check direct - 5.7+", //"4. arocknophobia - 5.7", "5. balls deep - 5.9+", "6. jingle bells - 5.9", "7. itching climb - 5.8-", "8. hog nose 5.10+", //"9. slab dance - 5.10", "10. don't screw disco - 5.11c"}; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //listadapter routeadapter = new customadapter(this, routes); //adapter refernce xml string array resourcde listadapter routeadapter = new customadapter(this, getresources().getstringarray(r.array.routelist)); final listview routelistview = (listview) findviewbyid(r.id.routelistview); routelistview.setadapter(routeadapter); routelistview.setonitemclicklistener( new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { // working way selecting listview row above commented out string array //string route = string.valueof(parent.getitematposition(position)); //attempt string route = string.valueof(getresources().getstringarray(r.array.routelist)[position]); if (route == "1. shark bait - 5.9") { intent intent = new intent(view.getcontext(), routedetails.class); intent.putextra("route", "shark bait"); startactivity(intent); } if (route == "information") { intent intent = new intent(view.getcontext(), routedetails.class); intent.putextra("route", "birdsboro rock climbing quarry"); startactivity(intent); } } } ); } }
try extract array final string[]
can access within listener.
you need equals
compare strings.
final string[] values = getresources().getstringarray(r.array.routelist); listadapter routeadapter = new customadapter(this, values); final listview routelistview = (listview) findviewbyid(r.id.routelistview); routelistview.setadapter(routeadapter); routelistview.setonitemclicklistener( new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { string route = values[position]; if (route.equals("1. shark bait - 5.9")) { intent intent = new intent(mainactivity.this, routedetails.class); intent.putextra("route", "shark bait"); startactivity(intent); }
Comments
Post a Comment