【发布时间】:2016-02-19 02:49:35
【问题描述】:
在活动 B 中有一个 spinner,其中的数据来自 MySQL(表格位置)。
活动 B
private ArrayList<String> froms;
private JSONArray resultFrom;
public void addItemsOnFrom() {
travelFrom = (Spinner) findViewById(R.id.travelFrom);
StringRequest stringRequest = new StringRequest(Configs.FROM_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
resultFrom = j.getJSONArray(Configs.JSON_ARRAY);
//Calling method getStudents to get the students from the JSON Array
getFrom(resultFrom);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
//Creating a request queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
//Adding request to the queue
requestQueue.add(stringRequest);
}
private void getFrom(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
froms.add(json.getString(Configs.TAG_LOCATION));
} catch (JSONException e) {
e.printStackTrace();
}
}
//Setting adapter to show the items in the spinner
travelFrom.setAdapter(new ArrayAdapter<String>(Add_Details_Information.this, android.R.layout.simple_spinner_dropdown_item, froms));
}
当点击保存button时,它会将选择的值(OFFICE)返回给活动AlistView。而在Activity A中,当列表被按下时,它会intent到Activity B。此时,Activity B中的spinner将首先显示被选中的项目(OFFICE)。
**Table location** // table location has 2 data
NONE
OFFICE
假设B中选择了OFFICE,点击列表时,我希望OFFICE首先显示在spinnerB中。
活动 B 中的代码,用于首先显示 OFFICE。
if(getIntent().getExtras()!=null)
{
final String from = getIntent().getStringExtra("from");
selectedItemFrom(from);
}
public void selectedItemFrom(final String value)// display OFFICE first
{
travelFrom = (Spinner) findViewById(R.id.travelFrom);
StringRequest stringRequest = new StringRequest(Configs.FROM_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(Configs.JSON_ARRAY);
//Calling method getStudents to get the students from the JSON Array
getFrom(result, value);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
//Creating a request queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
//Adding request to the queue
requestQueue.add(stringRequest);
}
private void getFrom(JSONArray j, String value) {
int position = 0;
//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
froms.add(json.getString(Configs.TAG_LOCATION));
if (froms.get(i).equalsIgnoreCase(value)) {
position = i;
//Toast.makeText(getApplicationContext(),position+"",Toast.LENGTH_LONG).show();
break;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
travelFrom.setAdapter(new ArrayAdapter<String>(Add_Details_Information.this, android.R.layout.simple_spinner_dropdown_item, froms));
travelFrom.setSelection(position);
}
OFFICE 可以先显示,但问题是当我检查微调器 B 时,它显示 NONE,OFFICE,NONE OFFICE ..为什么微调器数据会重复?谢谢
我认为问题出在这一行travelFrom.setAdapter(new ArrayAdapter<String>(Add_Details_Information.this, android.R.layout.simple_spinner_dropdown_item, froms));...但是如何解决???有人吗?
有时微调器会先显示所选项目,但有时不会...有什么更好的书写方式?
编辑
{"result":[{"name":"NONE"},{"name":"OFFICE"}]}
我现在将 forms.clear 放在两个 getFrom 方法的开头。但问题是当我选择 NONE 并返回 A,然后再次转到 B 时,微调器现在只有 NONE...
【问题讨论】:
-
只需在 getFrom(JSONArray j,String value) 中打印表单的大小。并在添加到列表之前清除数据 forms.clear();
-
@SreeReddyMenon 我在 try 块中添加了
Log.e("SIZE",froms.size()+"");但日志没有显示 -
好的。现在在 catch 里面做同样的事情。是打印系统.err..那种东西吗?
-
@SreeReddyMenon 没有。问题是我使用 Toast 显示大小,但没有显示 Toast。
-
@SreeReddyMenon 你能教我第二种方法吗?清除列表
标签: java android json listview spinner