私はアンドロイドを新しくしました。私はデータベースからスピナーでデータを取得しようとしています。しかし、なぜ私のリストがスピナーにセットされていないのかわかりません。非同期タスクを使用して動的にスピナーのコンテンツを設定する方法は?
私のコードは、助けてください、私のサービス・ハンドラ・クラスは
public class ServiceHandler {
static InputStream is = null;
static String response = null;
public final static int GET = 1;
public final static int POST = 2;
public ServiceHandler() {
}
/**
* Making service call
* @url - url to make request
* @method - http request method
* */
public String makeServiceCall(String url, int method) {
return this.makeServiceCall(url, method, null);
}
/**
* Making service call
* @url - url to make request
* @method - http request method
* @params - http request params
* */
public String makeServiceCall(String url, int method,
List<NameValuePair> params) {
try {
// http client
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpEntity httpEntity = null;
HttpResponse httpResponse = null;
// Checking http request method type
if (method == POST) {
HttpPost httpPost = new HttpPost(url);
// adding post params
if (params != null) {
httpPost.setEntity(new UrlEncodedFormEntity(params));
}
httpResponse = httpClient.execute(httpPost);
} else if (method == GET) {
// appending params to url
if (params != null) {
String paramString = URLEncodedUtils
.format(params, "utf-8");
url += "?" + paramString;
}
HttpGet httpGet = new HttpGet(url);
httpResponse = httpClient.execute(httpGet);
}
httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
response = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error: " + e.toString());
}
return response;
}
}
と
public class State {
private int id;
private String name;
public State(){}
public State(int id, String name){
this.id = id;
this.name = name;
}
public void setId(int id){
this.id = id;
}
public void setName(String name){
this.name = name;
}
public int getId(){
return this.id;
}
public String getName(){
return this.name;
}
}
ある
public class AddBasicDetail extends Fragment implements AdapterView.OnItemSelectedListener {
// array list for spinner adapter
private ArrayList<State> categoriesList;
ProgressDialog pDialog;
Spinner sp_state;
// API urls
// Url to create new category
private String URL_NEW_CATEGORY = "http://mandirdekhoo.com/app/md_eng/state.php";
public AddBasicDetail() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.add_basic_detail, container, false);
sp_state = (Spinner) v.findViewById(R.id.sp_state);
categoriesList = new ArrayList<State>();
// spinner item select listener
//sp_state.setOnItemSelectedListener((AdapterView.OnItemSelectedListener) getActivity());
new GetCategories().execute();
return v;
}
/**
* Adding spinner data
* */
private void populateSpinner() {
List<String> lables = new ArrayList<String>();
//txtCategory.setText("");
for (int i = 0; i < categoriesList.size(); i++) {
lables.add(categoriesList.get(i).getName());
}
// Creating adapter for spinner
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(getContext(),
android.R.layout.simple_spinner_item, lables);
// Drop down layout style - list view with radio button
spinnerAdapter .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
sp_state.setAdapter(spinnerAdapter);
}
/**
* Async task to get all food categories
* */
private class GetCategories extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getContext());
pDialog.setMessage("Fetching food categories..");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
ServiceHandler jsonParser = new ServiceHandler();
String json = jsonParser.makeServiceCall(URL_NEW_CATEGORY, ServiceHandler.GET);
Log.e("Response: ", "> " + json);
if (json != null) {
try {
JSONObject jsonObj = new JSONObject(json);
Log.e("my response: ", "> " + jsonObj);
if (jsonObj != null) {
JSONArray categories = jsonObj .getJSONArray("result");
for (int i = 0; i < categories.length(); i++) {
JSONObject catObj = (JSONObject) categories.get(i);
State cat = new State(catObj.getInt("id"),
catObj.getString("state_name"));
categoriesList.add(cat);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("JSON Data", "Didn't receive any data from server!");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (pDialog.isShowing())
pDialog.dismiss();
//populateSpinner();
}
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(
getContext(),
parent.getItemAtPosition(position).toString() + " Selected" ,
Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
}
です
役立ちます;'ラインを? –
onPostExecute()からpopulateSpinner()を呼び出す – Rasel
01-11 10:10:51.870 2471-2531 /? {"id": "1"、 "state_name": "Assam"、 "msg": "Successfully"、 "status":1}、{"id": " 1 "、" id ":" 3 "、" state_name ":" Chandigarh "、" msg ":" Successfully "、" msg ":"成功 "、"ステータス ":1}、{" id ":" 5 "、" status ":1}、{" id " msgstr "成功"、 "ステータス":1}、{"id": "6"、 "state_name": "Gujarat"、 "msg": "Successfully"、 "status" :状態 ":1}、{" id ":" 8 "、" state_name ":" 1 "}、{" id ":" 7 "、" state_name " "msg": "成功"、 "ステータス":1}]} – Reenu