java - 单击列表时复制微调器数据

标签 java android json listview spinner

在 Activity B 中有一个 spinner数据来自哪里 MySQL (表位置)。

Activity 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)返回给 Activity A listView .而在 Activity A 中,当列表被按下时,它会 Intent 到 Activity B。此时,Activity B 中的微调器将首先显示所选项目(OFFICE)。

**Table location**  // table location has 2 data
NONE 
OFFICE

假设在 B 中选择了 OFFICE。单击列表时,我希望 OFFICE 首先显示在 spinner 中。 B、

Activity 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可以先显示,问题是我检查spinner B的时候显示NONE,OFFICE,NONE OFFICE ..为什么微调器数据会重复?谢谢

我认为问题出在这一行 travelFrom.setAdapter(new ArrayAdapter<String>(Add_Details_Information.this, android.R.layout.simple_spinner_dropdown_item, froms)); ……可是怎么解决???有人吗?

而且spinner有时会先显示选中的item,有时又不会...有什么更好的写法?

编辑

{"result":[{"name":"NONE"},{"name":"OFFICE"}]}

我现在将 forms.clear 放在两个 getFrom 方法的开头。但问题是当我选择 NONE 并返回到 A,然后再次转到 B 时,微调器现在只有 NONE...

最佳答案

在这段代码中添加 froms.clear();

private void getFrom(JSONArray j) {
    //Traversing through all the items in the json array
    froms.clear();
    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));
}

关于java - 单击列表时复制微调器数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35462471/

相关文章:

java - 使用 Java8 排序并检查数字是否连续

java - 是否有任何现成的 Java 库可用于通过 TCP 传输图像?

ios - 将 JSON 中的图像添加到 ScrollView Swift 4

java - org.springframework.jms.support.converter.MappingJackson2MessageConverter 线程安全吗?

java - 在 Java 中哈希 json 并进行比较

java - Open Stream 和 CancellationException 导致线程锁

java - 循环中的 HttpClient

java - 如何解决通过Wi-Fi(android API)发现其他设备的问题?

android - 在 Android 应用程序中深度链接 Deezer(广播和艺术家)

java - 将 utf-8 字符添加到字符串数组中