嘿所有人所以我一直试图让这个微调器连接起来,但它不会调用onItemSelected侦听器。这是我一直看到的SO和每一个tute,但没有任何工作。
*****************代码
public class QuoteActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
public Spinner mCountrySpinner;
public Spinner mStateSpinner;
public Spinner mCitySpinner;
public Spinner mPayPointSpinner;
public Spinner mDeliveryCurrencySpinner;
public ArrayList<String> mCountryOptions = new ArrayList<>();
public ArrayList<String> mStateOptions = new ArrayList<>();
public ArrayList<String> mCityOptions = new ArrayList<>();
public ArrayList<String> mPayPointOptions = new ArrayList<>();
public ArrayList<String> mDeliveryCurrencyOptions = new ArrayList<>();
public String mSelectedCountry;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quote);
overridePendingTransition(R.anim.anim_slide_in_left, R.anim.anim_slide_out_left);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
actionBar.setTitle(R.string.Calculate_a_Quote);
mCountrySpinner = (Spinner) findViewById(R.id.country_spinner);
mStateSpinner = (Spinner) findViewById(R.id.state_province_spinner);
mCitySpinner = (Spinner) findViewById(R.id.city_spinner);
mPayPointSpinner = (Spinner) findViewById(R.id.pay_point_spinner);
mDeliveryCurrencySpinner = (Spinner) findViewById(R.id.delivery_currency_spinner);
mCountrySpinner.setOnItemSelectedListener(this);
populateSpinnerData(WebApiManager.FAKE_AGENT_ID);
}
@Override
public void onStart(){
super.onStart();
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.anim_slide_in_right, R.anim.anim_slide_out_right);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_quote, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home: // Respond to the action bar's Up/Home button
onBackPressed();
return true;
case R.id.quote_action:
Toast.makeText(this, "TODO: Get Quote", Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
public void populateSpinnerData(String agentID){
getPayPointApiInfo(agentID, Utility.payPointDataType.countryType.toString(), null);
ArrayAdapter<String> countryAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,mCountryOptions);
mCountrySpinner.setAdapter(countryAdapter);
}
private void convertData(JSONObject data,String dataType) {
try {
String status = data.getString(WebApiManager.KEY_STATUS);
if (status.equals("Ok")) {
if(dataType.equalsIgnoreCase(Utility.payPointDataType.countryType.toString())){
JSONArray info = data.getJSONArray(WebApiManager.KEY_CAPITAL_RESULT);
for(int i = 0; i < info.length();i++){
JSONObject row = info.getJSONObject(i);
if(row != null)
mCountryOptions.add(row.getString("value"));
}
}
if(dataType.equalsIgnoreCase(Utility.payPointDataType.stateType.toString())){
JSONArray info = data.getJSONArray(WebApiManager.KEY_CAPITAL_RESULT);
for(int i = 0; i < info.length();i++){
JSONObject row = info.getJSONObject(i);
if(row != null)
mStateOptions.add(row.getString("value"));
}
}
if(dataType.equalsIgnoreCase(Utility.payPointDataType.cityType.toString())){
JSONArray info = data.getJSONArray(WebApiManager.KEY_CAPITAL_RESULT);
for(int i = 0; i < info.length();i++){
JSONObject row = info.getJSONObject(i);
if(row != null)
mCityOptions.add(row.getString("value"));
}
}
if(dataType.equalsIgnoreCase(Utility.payPointDataType.payPointType.toString())){
JSONArray info = data.getJSONArray(WebApiManager.KEY_CAPITAL_RESULT);
for(int i = 0; i < info.length();i++){
JSONObject row = info.getJSONObject(i);
if(row != null)
mPayPointOptions.add(row.getString("value"));
}
}
if(dataType.equalsIgnoreCase(Utility.payPointDataType.deliveryCurrencyType.toString())){
JSONArray info = data.getJSONArray(WebApiManager.KEY_CAPITAL_RESULT);
for(int i = 0; i < info.length();i++){
JSONObject row = info.getJSONObject(i);
if(row != null)
mDeliveryCurrencyOptions.add(row.getString("value"));
}
}
}
} catch (JSONException e) {
Log.i("PendingQueue", e.getLocalizedMessage());
} catch (JsonSyntaxException jse) {
Log.i("PendingQueue", jse.getLocalizedMessage());
}
}
public void getPayPointApiInfo(String agentID, final String type, String country) {
WebApiManager.getPayPointInfo(this, agentID, type, country, new WebApiManager.OnResponseListener() {
@Override
public void onResponse(JSONObject response) {
Log.i(getClass().getSimpleName(), "Response = " + response.toString());
convertData(response, type);
}
}, new WebApiManager.OnErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i(getClass().getSimpleName(), "Error");
AlertDialog alert = new AlertDialog.Builder(QuoteActivity.this)
.setMessage(error.getCause().getMessage())
.setTitle(R.string.app_name)
.create();
alert.show();
}
});
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
********** XML
<LinearLayout
android:id="@+id/country_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="15dp">
<TextView
android:id="@+id/country_title_text"
android:layout_width="0dp"
android:layout_weight=".5"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:textAppearance="?android:textAppearanceMedium"
android:text="Country"/>
<Spinner
android:id="@+id/country_spinner"
android:layout_width="0dp"
android:layout_weight=".5"
android:layout_height="wrap_content" />
</LinearLayout>
我在这做错了什么?
答案 0 :(得分:1)
好的,所以我终于找到了这个问题的答案。问题在于数据是通过对getPayPointApiInfo()中的api进行Web调用来填充的。由于我在Web请求完成之前将微调器分配给空数组列表,因此未调用侦听器。所以我只需要把:)
private void convertData(JSONObject data,String dataType) {
try {
String status = data.getString(WebApiManager.KEY_STATUS);
if (status.equals("Ok")) {
if(dataType.equalsIgnoreCase(Utility.payPointDataType.countryType.toString())){
JSONArray info = data.getJSONArray(WebApiManager.KEY_CAPITAL_RESULT);
for(int i = 0; i < info.length();i++){
JSONObject row = info.getJSONObject(i);
if(row != null)
mCountryOptions.add(row.getString("value"));
}
ArrayAdapter<String> countryAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,mCountryOptions);
mCountrySpinner.setAdapter(countryAdapter);
}
if(dataType.equalsIgnoreCase(Utility.payPointDataType.stateType.toString())){
JSONArray info = data.getJSONArray(WebApiManager.KEY_CAPITAL_RESULT);
for(int i = 0; i < info.length();i++){
JSONObject row = info.getJSONObject(i);
if(row != null)
mStateOptions.add(row.getString("value"));
}
}
if(dataType.equalsIgnoreCase(Utility.payPointDataType.cityType.toString())){
JSONArray info = data.getJSONArray(WebApiManager.KEY_CAPITAL_RESULT);
for(int i = 0; i < info.length();i++){
JSONObject row = info.getJSONObject(i);
if(row != null)
mCityOptions.add(row.getString("value"));
}
}
if(dataType.equalsIgnoreCase(Utility.payPointDataType.payPointType.toString())){
JSONArray info = data.getJSONArray(WebApiManager.KEY_CAPITAL_RESULT);
for(int i = 0; i < info.length();i++){
JSONObject row = info.getJSONObject(i);
if(row != null)
mPayPointOptions.add(row.getString("value"));
}
}
if(dataType.equalsIgnoreCase(Utility.payPointDataType.deliveryCurrencyType.toString())){
JSONArray info = data.getJSONArray(WebApiManager.KEY_CAPITAL_RESULT);
for(int i = 0; i < info.length();i++){
JSONObject row = info.getJSONObject(i);
if(row != null)
mDeliveryCurrencyOptions.add(row.getString("value"));
}
}
}
} catch (JSONException e) {
Log.i("PendingQueue", e.getLocalizedMessage());
} catch (JsonSyntaxException jse) {
Log.i("PendingQueue", jse.getLocalizedMessage());
}
}
答案 1 :(得分:0)
现在尝试使用它:
mCountrySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(), "test "+position, Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});