我正在尝试使用json从url加载微调器值。
我使用的代码是
private class DownloadJSON extends AsyncTask<Void, Void, Void>
{
protected Void doInBackground(Void... params)
{
// Locate the WorldPopulation Class
world = new ArrayList<WorldPopulation>();
// Create an array to populate the spinner
worldlist = new ArrayList<String>();
// JSON file URL address
jsonobject = JSONfunctions.getJSONfromURL("http://");
// Log.v("Load Employees",jsonobject.toString());
try
{
// Locate the NodeList name
jsonarray = jsonobject.getJSONArray("Employees");
for (int i = 0; i < jsonarray.length(); i++)
{
jsonobject = jsonarray.getJSONObject(i);
WorldPopulation worldpop = new WorldPopulation();
worldpop.setRank(jsonobject.optString("FirstName"));
worldpop.setCountry(jsonobject.optString("LastName"));
worldpop.setPopulation(jsonobject.optString("EmpNo"));
// worldpop.setFlag(jsonobject.optString("flag"));
world.add(worldpop);
// Populate spinner with country names
worldlist.add(jsonobject.optString("FirstName") + "-" +jsonobject.optString("EmpNo"));
mySpinner.setAdapter(new ArrayAdapter<String>(VisitorEntry.this,android.R.layout.simple_spinner_dropdown_item, worldlist));
}
}
catch (Exception e)
{
// Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
此代码有时会显示结果,并且有时无法显示。我不理解这个问题,想知道是否有其他方法可以将url中的值提取到微调器中。 我没有显示值的时间,我得到以下错误:
04-27 14:19:25.979: W/System.err(17486): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
04-27 14:19:25.979: W/System.err(17486): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4611)
04-27 14:19:25.979: W/System.err(17486): at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:867)
04-27 14:19:25.979: W/System.err(17486): at android.view.ViewGroup.invalidateChild(ViewGroup.java:4066)
04-27 14:19:25.979: W/System.err(17486): at android.view.View.invalidate(View.java:10271)
04-27 14:19:25.979: W/System.err(17486): at android.view.View.invalidate(View.java:10226)
04-27 14:19:25.979: W/System.err(17486): at android.widget.AbsSpinner.resetList(AbsSpinner.java:153)
04-27 14:19:25.979: W/System.err(17486): at android.widget.AbsSpinner.setAdapter(AbsSpinner.java:104)
04-27 14:19:25.979: W/System.err(17486): at android.widget.Spinner.setAdapter(Spinner.java:382)
04-27 14:19:25.989: W/System.err(17486): at com.example.wepvms.VisitorEntry$DownloadJSON1.doInBackground(VisitorEntry.java:854)
04-27 14:19:25.989: W/System.err(17486): at com.example.wepvms.VisitorEntry$DownloadJSON1.doInBackground(VisitorEntry.java:1)
04-27 14:19:25.989: W/System.err(17486): at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-27 14:19:25.989: W/System.err(17486): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
04-27 14:19:25.989: W/System.err(17486): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-27 14:19:25.989: W/System.err(17486): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
04-27 14:19:25.989: W/System.err(17486): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
04-27 14:19:25.989: W/System.err(17486): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
04-27 14:19:25.989: W/System.err(17486): at java.lang.Thread.run(Thread.java:856)
答案 0 :(得分:0)
首先,您可以使用Arraylist来创建数据,而不是创建Baseadapter类 用于显示数据..
答案 1 :(得分:0)
答案 2 :(得分:0)
我建议尝试放
mySpinner.setAdapter(new ArrayAdapter<String>(VisitorEntry.this,android.R.layout.simple_spinner_dropdown_item, worldlist));
这个内部runonUiThread
就像这样
runOnUiThread(new Runnable() {
@Override
public void run() {
mySpinner.setAdapter(new ArrayAdapter<String>(VisitorEntry.this,android.R.layout.simple_spinner_dropdown_item, worldlist));
}
});