这里使用来自json url的动态值填充微调器。我在微调器中获得了值,但是当选择如下所示时它们是不可见的。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spintoolbaractivity);
toolbar = (Toolbar) findViewById(R.id.toolbar);
spinner_nav = (Spinner) findViewById(R.id.spinner_nav);
if (toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
new GetTeacher().execute();
}
private class GetTeacher extends AsyncTask<Void, Void, Void>
{
@Override
protected void onPreExecute()
{
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(SpinToolbarActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0)
{
Lclasslist = new ArrayList<String>();
JsonObjectRequest req = new JsonObjectRequest(
url_all_teacher, null, new Response.Listener<JSONObject>(){
@Override
public void onResponse(JSONObject jsonObj) {
Log.d("reponse", jsonObj.toString());
try {
if (jsonObj != null)
{
class_details = jsonObj.getJSONArray(TAG_class_details);
String loginToken=class_details.toString();
String fstr=loginToken.substring(1, loginToken.length() - 1);
JSONObject jsonObj2 = new JSONObject(fstr);
JSONObject d = class_details.getJSONObject(0);
int success2 =d.getInt(TAG_SUCCESS);
if (success2 == 1)
{
class_details2= jsonObj2.getJSONArray(TAG_class_details2);
// looping through All Products
for (int i = 0; i < class_details2.length(); i++)
{
JSONObject c = class_details2.getJSONObject(i);
// adding ArrayList and this list is added to spinner
Lclasslist.add(c.getString("class_id"));
}
}
else
{
//setnoclssp();
}
} else {
//Log.e("ServiceHandler", "Couldn't get any data from the url");
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
hidepDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG_class_details, "Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
hidepDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(req);
return null;
}
@Override
protected void onPostExecute (Void result)
{
addItemsToSpinner();
}
}
//动态地将项目添加到微调器中
public void addItemsToSpinner()
{
//自定义ArrayAdapter,使用微调器项布局设置弹出背景
CustomSpinnerAdapter spinAdapter = new CustomSpinnerAdapter(
getApplicationContext(), Lclasslist);
spinner_nav.setAdapter(spinAdapter);
spinner_nav.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapter, View v,
int position, long id) {
// On selecting a spinner item
String item = adapter.getItemAtPosition(position).toString();
// Showing selected spinner item
Toast.makeText(getApplicationContext(), "Selected : " + item,
Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
private void showpDialog() {
if (!pDialog.isShowing())
pDialog.show();
}
private void hidepDialog() {
if (pDialog.isShowing())
pDialog.dismiss();
}
答案 0 :(得分:0)
更改方法addItemsToSpinner()。
addItemsToSpinner():
public void addItemsToSpinner(){
final ArrayAdapter<String> adapter= new ArrayAdapter<>
(getApplicationContext(),android.R.layout.simple_spinner_item,Lclasslist);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp1.setAdapter(adapter);
}
在onCreate()
中使用onItemSelected outide这个方法sp1.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
String selectedItem = sp1.getSelectedItem().toString();
Toast.makeText(getApplicationContext(), selectedItem, Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
这应该有效,一切顺利。
答案 1 :(得分:-1)
首先,你不应该在适配器中实现itemSelectedListenet,否则你应该在oncreate活动方法中实现它。尝试将其放入oncreate并检查它是否有效。 此外,如果你只是想随时获得所选项目,那么点击你可能不需要onItemSelectedListenr而不是你可以使用
yourSpinner.getSelectedItem();
希望这会有所帮助。