我是android新手 我试图根据在微调器1中选择的值填充微调器2。 我能够在微调器2中加载字段,但没有选择值。
这是我的注册活动:
package com.example.ddubusmarg;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class SignUpActivity extends Activity implements OnItemSelectedListener {
// Progress Dialog
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
EditText inputName;
EditText inputId;
EditText inputPassword;
EditText inputConfirmPassword;
EditText inputPhoneNumber;
// url to create new product
private static String url_create_product = "http://192.168.1.6/registrationjson/insertnew.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
//spinner creation
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
Spinner spinner3 = (Spinner) findViewById(R.id.spinner3);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(this,
R.array.boarding_city, android.R.layout.simple_spinner_item);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this,
R.array.ahmedabad_routes, android.R.layout.simple_spinner_item);
ArrayAdapter<CharSequence> adapter3 = ArrayAdapter.createFromResource(this,
R.array.ahmedabad_RTO_boardings, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner1.setAdapter(adapter1);
spinner2.setAdapter(adapter2);
spinner3.setAdapter(adapter3);
spinner1.setOnItemSelectedListener(this);
spinner2.setOnItemSelectedListener(this);
spinner3.setOnItemSelectedListener(this);
// Edit Text
inputName = (EditText) findViewById(R.id.editText1); // name
inputId = (EditText) findViewById(R.id.editText2); //id
inputPassword = (EditText) findViewById(R.id.editText3); //password
inputConfirmPassword = (EditText) findViewById(R.id.editText4); //confirm password
//inputCity = (EditText) findViewById(R.id.spinner); boarding city
inputPhoneNumber = (EditText) findViewById(R.id.editText5); // phone number
// Create button
Button btnCreateProduct = (Button) findViewById(R.id.button1);
// button click event
btnCreateProduct.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//confirming password
String name = inputName.getText().toString();
String id = inputId.getText().toString();
String password = inputPassword.getText().toString();
String confirmPassword = inputConfirmPassword.getText().toString();
String phoneNumber = inputPhoneNumber.getText().toString();
if(name.equals("")||id.equals("")||password.equals("")||confirmPassword.equals("")||phoneNumber.equals(""))
{
Toast.makeText(getApplicationContext(), "Field(s) Vaccant", Toast.LENGTH_LONG).show();
return;
}
// check if both password matches
if(phoneNumber.length() != 10)
{
Toast.makeText(getApplicationContext(), "PhoneNumber is not 10 digits", Toast.LENGTH_LONG).show();
return;
}
if(!password.equals(confirmPassword))
{
Toast.makeText(getApplicationContext(), "Password Does Not Matches", Toast.LENGTH_LONG).show();
return;
}
else
{
// creating new product in background thread
new CreateNewProduct().execute();
}
}
});
}
/**
* Background Async Task to Create new product
* */
class CreateNewProduct extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(SignUpActivity.this);
pDialog.setMessage("Inserting Record...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Creating product
* */
//again spinner
protected String doInBackground(String... args) {
String name = inputName.getText().toString();
name = name.toUpperCase();
String id = inputId.getText().toString();
id = id.toUpperCase();
String password = inputPassword.getText().toString();
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
String city = spinner1.getSelectedItem().toString();
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
String route = spinner2.getSelectedItem().toString();
Spinner spinner3 = (Spinner) findViewById(R.id.spinner3);
String boarding = spinner3.getSelectedItem().toString();
String phoneNumber = inputPhoneNumber.getText().toString();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", name));
params.add(new BasicNameValuePair("id", id));
params.add(new BasicNameValuePair("password", password));
params.add(new BasicNameValuePair("city", city));
params.add(new BasicNameValuePair("route", route));
params.add(new BasicNameValuePair("boarding", boarding));
params.add(new BasicNameValuePair("phoneNumber", phoneNumber));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_create_product,
"GET", params);
// check log cat for response
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully created product
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
// closing this screen
finish();
} else {
// failed to create product
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}
}
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(this);
//Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
//spinner2.setOnItemSelectedListener(this);
//setting spinner2 based on spinner1
String city= String.valueOf(spinner1.getSelectedItem());
if(city.contentEquals("Ahmedabad")) {
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this,
R.array.ahmedabad_routes, android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter2.notifyDataSetChanged();
spinner2.setAdapter(adapter2);
spinner2.setOnItemSelectedListener(this);
}
else if(city.contentEquals("Vadodara")) {
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this,
R.array.vadodara_routes, android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter2.notifyDataSetChanged();
spinner2.setAdapter(adapter2);
spinner2.setOnItemSelectedListener(this);
}
Spinner spinner3 = (Spinner) findViewById(R.id.spinner3);
spinner3.setOnItemSelectedListener(this);
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
}
请帮助。
答案 0 :(得分:0)
你已经在onCreate()中注册过一次就足够了。在onItemSelected回调中再次删除注册。
更多OOP风格的推荐方法是拥有不同的侦听器对象。既然你有三个微调器的侦听器对象(活动本身),你需要在onItemSelected中基于view.getId()编写一个switch case,并为每个case执行不同的操作。
代码如下所示
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
switch(view.getId()){
case R.id.spinner1id:
//do spinner 1 action based on pos and id
case R.id.spinner2id:
//do spinner 2 action based on pos and id
case R.id.spinner3id:
//do spinner 3 action based on pos and id
}
}