gpt4 book ai didi

android - 如何使用异步任务动态设置微调器中的内容?

转载 作者:太空狗 更新时间:2023-10-29 14:44:58 24 4
gpt4 key购买 nike

我是安卓新手。我正在尝试从数据库中获取微调器中的数据。但是我不知道为什么我的列表没有在微调器中设置。

我的代码是

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) {
}
}

我的服务处理器类是

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;
}
}

请帮忙

最佳答案

嘿,在得到正确的响应后尝试添加这段代码

例如,我正在尝试硬编码值。将其替换为您的 json。

   final List<String> yourlist=new ArrayList<String>();
yourlist.add("Item 1");
yourlist.add("Item 2");
yourlist.add("Item 3");
yourlist.add("Item 4");
yourlist.add("Item 5");
final Spinner sp1= (Spinner) findViewById(R.id.spinner1);
final Spinner sp2= (Spinner) findViewById(R.id.spinner2);

ArrayAdapter<String> adp1=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,yourlist);
adp1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp1.setAdapter(adp1);

希望这有帮助。:)

关于android - 如何使用异步任务动态设置微调器中的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41582766/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com