1
我是新手android 我想填充微調器2基於微調器1中選擇的值。 我能夠加載微調器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
}
}
好心的幫助。
u能約父視圖POS和id解釋。我明白,適配器類用於填充字符串數組內容,並將適配器設置爲微調器,它用填充字符串數組填充。 spinner.setOnItemSelectedListener()調用onItemSelected()方法。在微調器被設置並且用戶改變選擇之後,對此onItemSelected方法進行調用。我無法找到視圖父母的身份證號碼。我做過實驗,但我不明白它們的功能。請幫助我找出基於另一個微調器的微調器的填充。 – 2014-11-04 04:54:42
父\t - 選擇發生的AdapterView。 視圖 - 單擊的AdapterView中的視圖。 position - 視圖在適配器中的位置。 id - 所選項目的行ID。 – Aun 2014-11-04 05:17:03
這取決於開發者如何連接回調。他可以使用任何/所有參數。比如說 - 你手中的數據結構代表了數據。因此,您可以使用位置信息作爲數據模型的索引來獲取用戶的選擇信息。 – Aun 2014-11-04 05:22:53