Я получил ответ от сервера, и я хочу установить значения для первого, второго и третьего счетчиков. Я могу установить значения для первого spinner only.but, но я не получил решение для отображения значений для оставшихся двух прядильщиков. Он выглядит как черный/пустой. Я не знаю, где проблема. может ли кто-нибудь найти решение для этого и дайте мне знать.Как получить данные с сервера и поместить данные в прядильщики
Вот мой код.
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 ответ:
{ "Ответ": { "Успех": "1" "Результат": [{ "ID": "1", "имя" : «Продовольственные товары & « Удобство »}, {" id ":" 2 "," name ":" Pharmacy "}, {" id ":" 3 "," name ":" Fashion & Принадлежности "}, { «id»: «4», «name»: «Electronics»}, {«id»: «5», «name»: «Beauty & Wellness»}, {"id": "6", "name" : «Food»}, {«id»: «7», «name»: «Books & Игры»}, {"id": "8", "name ":" Для детей "}, {" id ":" 9 "," name ":" Home & Living "}, {" id ":" 10 "," name ":" Travel & Hotels "}]} }
{"Response": {"Success": "1", "Result": [{"city": "Abuja"}, {"city": "Enugu"}, {"city": " Kano "}, {" city ":" Lagos "}}}}
{" Response ": {" Success ":" 1 "," Result ": [{" area ":" Lekki "}]} }
Не могли бы вы более подробно объяснить, какова будет ваша проблема? Что вы подразумеваете под «Здесь я вставлю свой код в мой ответ от сервера»? – SaschaM78
SaschaM78 Я получил ответ от server.i взял 3 spinners.I установил dta на три spinners.for первый spinner только я получил data.i не получил данные, чтобы остальные два spinners.it выглядит как пустой –
@SarathNadela положить отлаживать здесь: listnew.add (jsonobject.optString ("город")); sp.add (jsonobject.optString ("area")); и проверьте, получите ли вы данные здесь –