我是Android中的新手。我想要存储微调器选择的项目ID,它保存在mysql数据库中。我已经将mysql数据库中的项目检索到我的spinner.Now我想将该微调器选择的项目id存储在全局变量中。请帮我。 这是我的代码
AddProject
RequestQueue requestQueue;
private ArrayList<Category> categoriesList;
// Url to get all categories
private String URL_COMPANY = "http://www.mybusket.com/pmsapp/webs/get_all_company.php";
//Data add
private String INSERT_URL = "http://192.168.1.9:8082/pmsapp/pmsemp/webapi/project_add.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_project);
Spinner = (Spinner) findViewById(R.id.spinner);
ProjectName = (EditText)findViewById(R.id.projectName);
IsBilling = (EditText)findViewById(R.id.IsBilling);
BillingBy = (EditText)findViewById(R.id.billingBy);
ProjectDes = (EditText)findViewById(R.id.projectDescription);
Save = (Button)findViewById(R.id.save);
requestQueue = Volley.newRequestQueue(getApplicationContext());
categoriesList = new ArrayList<Category>();
// spinner item select listener
Spinner.setOnItemSelectedListener(this);
new GetCompany().execute();
}
/**
* Adding spinner data
* */
private void populateSpinner() {
List<String> lables = new ArrayList<String>();
for (int i = 0; i < categoriesList.size(); i++) {
lables.add(categoriesList.get(i).getName());
}
// Creating adapter for spinner
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
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
Spinner.setAdapter(spinnerAdapter);
}
private class GetCompany extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... voids) {
ServiceHandler jsonParser = new ServiceHandler();
String json = jsonParser.makeServiceCall(URL_COMPANY, ServiceHandler.GET);
Log.e("Response: ", "> " + json);
if (json != null) {
try {
JSONObject jsonObj = new JSONObject(json);
if (jsonObj != null) {
JSONArray categories = jsonObj.getJSONArray("categories");
for (int i = 0; i < categories.length(); i++) {
JSONObject catObj = (JSONObject) categories.get(i);
Category cat = new Category(catObj.getInt("companyid"),
catObj.getString("companyname"));
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);
populateSpinner();
}
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//Toast.makeText(getApplicationContext(), parent.getItemAtPosition(position).toString() + " Selected" , Toast.LENGTH_LONG).show();
}
我想在从微调器中选择一个项目时存储该公司ID。请帮助我。
答案 0 :(得分:1)
使用此代码即可获得ID
categoriesList.get(Spinner.getSelectedItemPosition()).getId();
Ex:在Spinner onItemSelected方法
中写下代码@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), categoriesList.get(position).getId() + " Selected" , Toast.LENGTH_LONG).show();
}