我收到了服务器的响应,我想设置第一个,第二个和第三个微调器的值。我只能为第一个微调器设置值。但我没有得到显示剩余两个微调器值的解决方案。它显示为黑色/空白。我不知道问题出在哪里。任何人都可以找到解决方案,让我知道。
这是我的代码。
public class Send extends Fragment {
Spinner sp1, sp2, sp3;
Button b1, b2;
Bitmap bmp;
String image;
ImageView iview;
Intent i = new Intent();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.send, container, false);
sp1 = (Spinner) v.findViewById(R.id.categories);
sp2 = (Spinner) v.findViewById(R.id.selectCity);
sp3 = (Spinner) v.findViewById(R.id.selectArea);
b1 = (Button) v.findViewById(R.id.search);
b2 = (Button) v.findViewById(R.id.clear);
new DownloadJSON().execute();
/* new MyTask().execute(); */
return v;
}
// Download JSON file AsyncTask
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
JSONObject jsonobject;
JSONArray jsonarray;
ArrayList<String> worldlist;
ArrayList<WorldPopulation> world;
ArrayList<String> listnew;
ArrayList<String> sp;
ProgressDialog pDialog;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Fetching food categories..");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// Locate the WorldPopulation Class
world = new ArrayList<WorldPopulation>();
// Create an array to populate the spinner
worldlist = new ArrayList<String>();
listnew = new ArrayList<String>();
sp = new ArrayList<String>();
// JSON file URL address
jsonobject = JSONfunctions
.getJSONfromURL("http://creersoft.com/webservices/ajaxData.php?category_id=1&city_id=Lagos");
try {
JSONObject maJsonObject = jsonobject.getJSONObject("Response");
JSONArray jsonArray = maJsonObject.getJSONArray("Result");
for (int i = 0; i < jsonArray.length(); i++) {
jsonobject = jsonArray.getJSONObject(i);
WorldPopulation worldpop = new WorldPopulation();
worldpop.setId(jsonobject.optInt("id"));
worldpop.setName(jsonobject.optString("name"));
worldpop.setName(jsonobject.optString("city"));
worldpop.setArea(jsonobject.optString("area"));
world.add(worldpop);
// Populate spinner with country names
worldlist.add(jsonobject.optString("name"));
listnew.add(jsonobject.optString("city"));
sp.add(jsonobject.optString("area"));
}
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
// Locate the spinner in activity_main.xml
if (pDialog.isShowing())
pDialog.dismiss();
// Spinner adapter
sp1.setAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_dropdown_item, worldlist));
sp2.setAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_dropdown_item, listnew));
sp3.setAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_dropdown_item_1line, sp));
// Spinner on item click listener
sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long arg3) {
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
sp2.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}
}
这是我的JSON响应:
{&#34;响应&#34; {&#34;成功&#34;:&#34; 1&#34;&#34;结果&#34;:[{&#34; ID&#34 ;:&#34; 1&#34;,&#34; name&#34;:&#34; Groceries&amp; 方便&#34;},{&#34; id&#34;:&#34; 2&#34;,&#34;名称&#34;:&#34;药房&#34;},{&#34; ID&#34;:&#34; 3&#34;&#34;名称&#34;:&#34;时装 &安培; 配件&#34;},{&#34; ID&#34;:&#34; 4&#34;&#34;名称&#34;:&#34;电子&#34;},{&#34; ID& #34;:&#34; 5&#34;&#34;名称&#34;:&#34;美容 &安培;健康&#34;},{&#34; ID&#34;:&#34; 6&#34;&#34;名称&#34;:&#34;食物&#34;},{&#34; ID& #34;:&#34; 7&#34;,&#34; name&#34;:&#34; Books&amp; 游戏&#34;},{&#34; id&#34;:&#34; 8&#34;,&#34;名称&#34;:&#34;对于儿童&#34;},{&#34; id&#34;:&#34; 9&#34;,&#34; name&#34;:&#34; Home&amp; 生活&#34;},{&#34; id&#34;:&#34; 10&#34;,&#34; name&#34;:&#34; Travel&amp;旅馆&#34;}]}}
{&#34;响应&#34; {&#34;成功&#34;:&#34; 1&#34;&#34;结果&#34;:[{&#34;城市&#34 ;:&#34;阿布贾&#34;},{&#34;城市&#34;:&#34;埃努古&#34;},{&#34;城市&#34;:&#34;卡诺&#34; },{&#34;城市&#34;:&#34;拉各斯&#34;}]}}
{&#34;响应&#34; {&#34;成功&#34;:&#34; 1&#34;&#34;结果&#34;:[{&#34;区域&#34 ;:&#34;莱基&#34;}]}}
答案 0 :(得分:1)
我认为JSONArray jsonArray = maJsonObject.getJSONArray("Result");
只是
"Result":[{"id":"1","name":"Groceries & Convenience"},{"id":"2","name":"Pharmacy "},{"id":"3","name":"Fashion & Accessories"},{"id":"4","name":"Electronics"},{"id":"5","name":"Beauty & Wellness"},{"id":"6","name":"Food"},{"id":"7","name":"Books & Games"},{"id":"8","name":"For Kids"},{"id":"9","name":"Home & Living"},{"id":"10","name":"Travel & Hotels"}]
并且没有city
属性。您应该单独迭代其他Response
json对象,以使其他结果包含city
和area
属性。