i tried to develop currency converter application in android. i get error in the ArrayAdapter, -
it shows error in next line
arrayadapter<charsequence> adapter = arrayadapter.createfromresource( this, r.array.name, android.r.layout.simple_spinner_item); adapter.setdropdownviewresource(android.r.layout.select_dialog_singlechoice); val = getresources().getstringarray(r.array.value);
the error msg "array cannot resolved or not field".
import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.client.clientprotocolexception; import org.apache.http.client.httpclient; import org.apache.http.client.methods.httpget; import org.apache.http.impl.client.defaulthttpclient; import org.json.jsonexception; import org.json.jsonobject; import android.app.activity; import android.content.intent; import android.net.uri; import android.os.bundle; import android.os.handler; import android.view.view; import android.view.view.onclicklistener; import android.widget.adapterview; import android.widget.adapterview.onitemselectedlistener; import android.widget.arrayadapter; import android.widget.button; import android.widget.spinner; import android.widget.textview; import android.widget.toast; public class mainactivity extends activity { public int to; public int from; public string [] val; public string s; public handler handler; /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); spinner s1 = (spinner) findviewbyid(r.id.spinner1); spinner s2 = (spinner) findviewbyid(r.id.spinner2); arrayadapter<charsequence> adapter = arrayadapter.createfromresource( this, r.array.name, android.r.layout.simple_spinner_item); adapter.setdropdownviewresource(android.r.layout.select_dialog_singlechoice); val = getresources().getstringarray(r.array.value); s1.setadapter(adapter); s2.setadapter(adapter); s1.setonitemselectedlistener(new spinone(1)); s2.setonitemselectedlistener(new spinone(2)); button b = (button) findviewbyid(r.id.button1); b.setonclicklistener(new onclicklistener(){ public void onclick(view v) { textview t = (textview) findviewbyid(r.id.textview4); if(from == to) { toast.maketext(getapplicationcontext(), "invalid", 4000).show(); } else { seek { s = getjson("http://query.yahooapis.com/v1/public /yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22"+val[from]+val[to]+"%22)&format=json&diagnostics=true&env=store%3a%2f%2fdatatables.org%2falltableswithkeys&callback="); jsonobject jobj; jobj = new jsonobject(s); string exresult= jobj.getjsonobject("query").getjsonobject("results").getjsonobject("rate").getstring("rate" ); t.settext(exresult); } grab (jsonexception e) { // todo auto-generated grab block e.printstacktrace(); } grab (clientprotocolexception e) { // todo auto-generated grab block e.printstacktrace(); } grab (ioexception e) { // todo auto-generated grab block e.printstacktrace(); } } } }); textview credit = (textview) findviewbyid(r.id.tv1); credit.setonclicklistener(new onclicklistener(){ @override public void onclick(view v) { intent = new intent(intent.action_view); i.setdata(uri.parse("http://finance.yahoo.com")); startactivity(i); } }); } public string getjson(string url)throws clientprotocolexception, ioexception { stringbuilder build = new stringbuilder(); httpclient client = new defaulthttpclient(); httpget httpget = new httpget(url); httpresponse response = client.execute(httpget); httpentity entity = response.getentity(); inputstream content = entity.getcontent(); bufferedreader reader = new bufferedreader(new inputstreamreader(content)); string con; while ((con = reader.readline()) != null) { build.append(con); } homecoming build.tostring(); } private class spinone implements onitemselectedlistener { int ide; spinone(int i) { ide =i; } public void onitemselected(adapterview<?> parent, view view, int index, long id) { if(ide == 1) = index; else if(ide == 2) = index; } public void onnothingselected(adapterview<?> arg0) { // todo auto-generated method stub } } }
in res/values/strings u need declare ur arrays name , value :
<string-array name="name"> <item>name 1</item> <!-- set values u required in array --> <item>name 2</item> </string-array> <string-array name="value"> <item>value1</item> <!-- set values u required in array --> <item>value2</item> </string-array>
android
No comments:
Post a Comment