Android 如何使微调器选择值基于先前选择的值?
我在活动a上有一个微调器,微调器值取自数据库中的一个表。当用户从微调器中选择该值时,该值将保存到同一数据库但不同的表中 然后有一个按钮指向下一个活动,在下一个活动中有另一个微调器,但我需要微调器选择值来显示我在上一个活动中选择的内容 Android代码:Android 如何使微调器选择值基于先前选择的值?,android,android-volley,spinner,Android,Android Volley,Spinner,我在活动a上有一个微调器,微调器值取自数据库中的一个表。当用户从微调器中选择该值时,该值将保存到同一数据库但不同的表中 然后有一个按钮指向下一个活动,在下一个活动中有另一个微调器,但我需要微调器选择值来显示我在上一个活动中选择的内容 Android代码: String text; String url = "http:///10.92.5.51/test/get.php"; String spinURL = "http://10.92.5.51/test/getSpin.php"; priva
String text;
String url = "http:///10.92.5.51/test/get.php";
String spinURL = "http://10.92.5.51/test/getSpin.php";
private JSONArray result;
private ArrayList<String> districts;
private ArrayList<Integer> position;
Spinner spinex;
TextView spintext;
Button uploadBtn;
Integer r;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
districts = new ArrayList<String>();
spinex = (Spinner) findViewById(R.id.spiner);
spintext = findViewById(R.id.spinText);
uploadBtn = findViewById(R.id.btnUpload);
getData();
spinex.post(new Runnable() {
@Override
public void run() {
spinex.setSelection(4);
}
});
getPosition();
}
private void getData() {
StringRequest stringRequest = new StringRequest(url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
JSONObject j = null;
try {
//Parsing the fetched Json String to JSON Object
j = new JSONObject(response);
//Storing the Array of JSON String to our JSON Array
result = j.getJSONArray("result");
getDistrict(result);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
//Creating a request queue
MySingleton.getInstance(this).addToRequestQueue(stringRequest);
}
private void getPosition() {
StringRequest stringRequest = new StringRequest(spinURL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
JSONObject j = null;
try {
//Parsing the fetched Json String to JSON Object
j = new JSONObject(response);
//Storing the Array of JSON String to our JSON Array
result = j.getJSONArray("result");
getSpinPosition(result);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
//Creating a request queue
MySingleton.getInstance(this).addToRequestQueue(stringRequest);
}
private void getDistrict(JSONArray j) {
//Traversing through all the items in the json array
for (int i = 0; i < j.length(); i++) {
try {
//Getting json object
JSONObject json = j.getJSONObject(i);
//Adding the name of the student to array list
districts.add(json.getString("city_name"));
} catch (JSONException e) {
e.printStackTrace();
}
}
spinex.setAdapter(new ArrayAdapter<String>(Main2Activity.this, android.R.layout.simple_spinner_dropdown_item, districts));
}
private void getSpinPosition(JSONArray j) {
//Traversing through all the items in the json array
try {
//Getting json object
JSONObject json = j.getJSONObject(1);
//Adding the name of the student to array list
r = Integer.valueOf(json.getString("city_id"));
} catch (JSONException e) {
e.printStackTrace();
}
}
字符串文本;
字符串url=”http:///10.92.5.51/test/get.php";
字符串spinURL=”http://10.92.5.51/test/getSpin.php";
私人JSONArray结果;
私人住宅区;
私人ArrayList职位;
Spinner spinex;
文本视图;文本;
按钮上传;
整数r;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
districts=新的ArrayList();
spinex=(微调器)findViewById(R.id.spiner);
spintext=findViewById(R.id.spintext);
uploadBtn=findViewById(R.id.btnUpload);
getData();
spinex.post(新的Runnable(){
@凌驾
公开募捐{
尖晶石(4);
}
});
getPosition();
}
私有void getData(){
StringRequest StringRequest=新的StringRequest(url,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
JSONObject j=null;
试一试{
//将获取的Json字符串解析为Json对象
j=新的JSONObject(响应);
//将JSON字符串数组存储到我们的JSON数组中
结果=j.getJSONArray(“结果”);
getDistrict(结果);
}捕获(JSONException e){
e、 printStackTrace();
}
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
}
});
//创建请求队列
getInstance(this).addToRequestQueue(stringRequest);
}
私人职位(){
StringRequest StringRequest=新的StringRequest(spinURL,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
JSONObject j=null;
试一试{
//将获取的Json字符串解析为Json对象
j=新的JSONObject(响应);
//将JSON字符串数组存储到我们的JSON数组中
结果=j.getJSONArray(“结果”);
获取旋转位置(结果);
}捕获(JSONException e){
e、 printStackTrace();
}
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
}
});
//创建请求队列
getInstance(this).addToRequestQueue(stringRequest);
}
私人住宅区(JSONArray j){
//遍历json数组中的所有项
对于(int i=0;i
以下是php代码:
<?php
require_once('DBConnect.php');
$sql = "select * from city ORDER BY city_id ASC";
$res = mysqli_query($con,$sql);
$result = array();
while($row = mysqli_fetch_array($res)){
array_push($result,
array(
'city_id'=>$row['city_id'],
'city_name'=>$row['city_name'],
)
);
}
echo json_encode(array("result"=>$result));
mysqli_close($con);
将结果保存到数据库中,然后从数据库中获取结果activity@GuanHongHuang你能调整代码吗?我是android开发新手。