简体   繁体   English

在 android studio 中使用 Mysql 填充基于国家微调器的城市微调器

[英]Populate city spinner base on country spinner using Mysql in android studio

I'm a beginner on Android applications.我是 Android 应用程序的初学者。 I Need Help I am doing this for the last 4 day我需要帮助 我过去 4 天都在做这件事

if I select Mumbai, the 2nd spinner should display the respected pickup_point_cd (show 4 value not Airport value :D)如果我选择孟买,第二个微调器应该显示受人尊敬的pickup_point_cd(显示 4 值而不是机场值:D)

在此处输入图片说明

public class MainActivity extends AppCompatActivity implements Spinner.OnItemSelectedListener{公共类 MainActivity 扩展 AppCompatActivity 实现 Spinner.OnItemSelectedListener{

//Declaring an Spinner
public Spinner spinner;
public Spinner spinner1;

//An ArrayList for Spinner Items
public ArrayList<String> students;
public ArrayList<String> students1;


//JSON Array
public JSONArray result;
public JSONArray result1;

//TextViews to display details
private TextView textViewName;
private TextView textViewCourse;
private TextView textViewSession;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Initializing the ArrayList
    students = new ArrayList<String>();
    students1 = new ArrayList<String>();

    //Initializing Spinner
    spinner = (Spinner) findViewById(R.id.spinner);
    spinner1 = (Spinner) findViewById(R.id.spinner2);


    //Adding an Item Selected Listener to our Spinner
    //As we have implemented the class Spinner.OnItemSelectedListener to this class iteself we are passing this to setOnItemSelectedListener
    spinner.setOnItemSelectedListener(this);
    spinner1.setOnItemSelectedListener(this);

    //Initializing TextViews
    textViewName = (TextView) findViewById(R.id.textViewName);
    textViewCourse = (TextView) findViewById(R.id.textViewCourse);
    textViewSession = (TextView) findViewById(R.id.textViewSession);

    //This method will fetch the data from the URL
    getData();
    getData1();



}

public void getData(){
    //Creating a string request
    StringRequest stringRequest = new StringRequest(Config.DATA_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(Config.JSON_ARRAY);

                        //Calling method getStudents to get the students from the JSON Array
                        getStudents(result);
                    } 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);
}

public void getStudents(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
            students.add(json.getString(Config.TAG_USERNAME));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

public void getData1() {
    //Creating a string request
    StringRequest stringRequest = new StringRequest(Config.DATA2_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
                        result1 = j.getJSONArray(Config.JSON_ARRAY2);

                        //Calling method getStudents to get the students from the JSON Array
                        getStudents1(result1);
                    } 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);
}

public void getStudents1(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
            students1.add(json.getString(Config.TAG_CD));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    //Setting adapter to show the items in the spinner
    spinner.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, students));
    spinner1.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, students1));

}

Firstly you have to make web service or web api for fetching the required data for spinners in android application from your MySQL.首先,您必须制作 Web 服务或 Web api,以便从您的 MySQL 中获取 android 应用程序中微调器所需的数据。 After that you can get data through web service.之后,您可以通过 Web 服务获取数据。 Make 2 differnet web service for 2 diff spinner data.为 2 个差异微调器数据制作 2 个不同的网络服务。 For reference you can use this tutorial.... enter link description here作为参考,您可以使用本教程.... 在此处输入链接描述

If you do these things I will provide you the code for how to set second spinner data on selection value of first spinner.如果您执行这些操作,我将为您提供有关如何在第一个微调器的选择值上设置第二个微调器数据的代码。 Here is the code.....这是代码......

Remove this lines first..
//Setting adapter to show the items in the spinner
spinner.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, students));
spinner1.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, students1));

and add these lines of code after getData() method并在 getData() 方法之后添加这些代码行

ArrayList<String> cityList_final = new ArrayList<String>();
        cityList_final .add("Select Item");
        cityList_final .addAll(students);
 spinner.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, cityList_final ));

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                                       int pos, long arg3) {

                spinnerSelectedItem= spinner.getItemAtPosition(pos).toString();
if(spinnerSelectedItem.equalsIgnoreCase("Select Item")==false) {                 
// call here your getData1 method and pass spinnerSelectedItem in url to fetch data according to spinnerSelectedItem.//donot call getData1 method in onCreate
                  getData1(spinnerSelectedItem);

            }

                setSecondSpinner();
            }

            public void onNothingSelected(AdapterView<?> arg0) {

            }
        });
public void setSecondSpinner()
{
 ArrayList<String> pickPointList_final = new ArrayList<String>();
    pickPointList_final .add("Select Item");
        pickPointList_final .addAll(students1);

 spinner1.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, pickPointList_final));
    spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                                   int arg2, long arg3) {


            spinnerSelectedItem1= spinner1.getItemAtPosition(arg2).toString();


        }

        public void onNothingSelected(AdapterView<?> arg0) {

        }
    });

}

Hope this will help you.....希望能帮到你.....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM