添加提示到spinner android

时间:2016-09-02 09:25:30

标签: android android-spinner

我在Web服务中添加微调器值。我想要的是在edittext中添加一个提示,例如"选择你的国家"。我尝试使用自定义适配器,但它没有成功。有什么好办法吗?

public class MainActivity extends AppCompatActivity {

    private EditText card_number_et, cvv_et, card_holder_name_et, multiline_et, postcode_et;
    private Spinner country_spin, state_spin, city_spin;
    private Button pay_btn;
    static EditText expiry_date_et;
    private ArrayAdapter adapter, adapter1, adapter2, adapter3;
    private ArrayList<String> namelist = new ArrayList<>();
    private ArrayList<String> idlist = new ArrayList<>();
    private HashMap<String,String> spinnerMap1, spinnerMap2, spinnerMap3;
    private String countryid, stateid, cityid;

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

        country_spin = (Spinner)findViewById(R.id.country_spin);
        state_spin = (Spinner)findViewById(R.id.state_spin);
        city_spin = (Spinner)findViewById(R.id.city_spin);

        card_number_et = (EditText)findViewById(R.id.card_number_et);
        expiry_date_et = (EditText)findViewById(R.id.expiry_date_et);
        cvv_et = (EditText)findViewById(R.id.cvv_et);
        card_holder_name_et = (EditText)findViewById(R.id.card_holder_name_et);
        multiline_et = (EditText)findViewById(R.id.multiline_et);
        postcode_et = (EditText)findViewById(R.id.postcode_et);

        pay_btn = (Button)findViewById(R.id.pay_btn);




        expiry_date_et.setOnClickListener(new View.OnClickListener() {
            @TargetApi(Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onClick(View v) {


                DatePickerFragment date = new DatePickerFragment();
                date.show(getFragmentManager(), "Date");

            }
        });



        country_spin.setPrompt("Select your country");
        state_spin.setPrompt("Select your state");
        city_spin.setPrompt("Select your city");

        getCountryList();


        country_spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                String countryname = country_spin.getSelectedItem().toString();
                System.out.println("somucountryname"+" "+countryname);
                countryid = spinnerMap1.get(countryname);
                System.out.println("somucountrynameid"+" "+countryid);
                if(countryid != null && !countryid.trim().isEmpty()){
                    getStateList(countryid);
                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        state_spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                String statename = state_spin.getSelectedItem().toString();
                System.out.println("somustatename"+" "+statename);
                stateid = spinnerMap2.get(statename);
                System.out.println("somustateid"+" "+stateid);
                if(stateid != null && !stateid.trim().isEmpty()){
                    getCityList(stateid);
                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        pay_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                makepayment();
            }
        });



    } // onCreate ends


    private void getCountryList() {

        if(namelist!=null )namelist.clear();
        if(idlist!=null )idlist.clear();

        StringRequest stringRequest = new StringRequest(Request.Method.GET, "http://www.example.com/webservice/allreligionlisting",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {


                        try {
                            JSONObject jObj = new JSONObject(response);
                            JSONArray jArray = jObj.getJSONArray("all_country");
                            for(int i=0;i<jArray.length();i++){
                                JSONObject main = jArray.getJSONObject(i);

                                namelist.add(main.getString("name"));
                                idlist.add(main.getString("code"));

                                String[] spinnerArray = new String[idlist.size()];
                                spinnerMap1 = new HashMap<>();
                                for (int j = 0; j < idlist.size(); j++)
                                {
                                    spinnerMap1.put(namelist.get(j),idlist.get(j));
                                    spinnerArray[j] = namelist.get(j);
                                }

                                adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_spinner_item, spinnerArray);
                                adapter.setDropDownViewResource(R.layout.spinnerlayout);
                                country_spin.setAdapter(adapter);
                            }




                        } catch (JSONException e) {
                            // JSON error
                            e.printStackTrace();
                            Toast.makeText(MainActivity.this, "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(MainActivity.this, "VolleyError" + error.toString(), Toast.LENGTH_LONG).show();

            }
        }) {
            /*@Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();


                return params;
            }*/

        };

        RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
        requestQueue.add(stringRequest);


    }

    private void getStateList(final String countryId) {

        if(namelist!=null )namelist.clear();
        if(idlist!=null )idlist.clear();

        StringRequest stringRequest = new StringRequest(Request.Method.GET, "http://www.example.com/webservice/allstate/"+countryId,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {


                        try {
                            JSONObject jObj = new JSONObject(response);

                            if(jObj.getString("ACK").equals("1")){

                                JSONArray jArray = jObj.getJSONArray("all_state");
                                for(int i=0;i<jArray.length();i++){
                                    JSONObject main = jArray.getJSONObject(i);

                                    namelist.add(main.getString("name"));
                                    idlist.add(main.getString("id"));

                                    String[] spinnerArray = new String[idlist.size()];
                                    spinnerMap2 = new HashMap<>();
                                    for (int j = 0; j < idlist.size(); j++)
                                    {
                                        spinnerMap2.put(namelist.get(j),idlist.get(j));
                                        spinnerArray[j] = namelist.get(j);
                                    }

                                    adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_spinner_item, spinnerArray);
                                    adapter.setDropDownViewResource(R.layout.spinnerlayout);
                                    state_spin.setAdapter(adapter);
                                }

                            }






                        } catch (JSONException e) {
                            // JSON error
                            e.printStackTrace();
                            Toast.makeText(MainActivity.this, "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(MainActivity.this, "VolleyError" + error.toString(), Toast.LENGTH_LONG).show();

            }
        }) {
            /*@Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();


                return params;
            }*/

        };

        RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
        requestQueue.add(stringRequest);


    }

    private void getCityList(final String stateId) {

        if(namelist!=null )namelist.clear();
        if(idlist!=null )idlist.clear();

        StringRequest stringRequest = new StringRequest(Request.Method.GET, "http://www.example.com/webservice/allcity/"+stateId,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {


                        try {
                            JSONObject jObj = new JSONObject(response);

                            if(jObj.getString("ACK").equals("1")){
                                JSONArray jArray = jObj.getJSONArray("all_city");
                                for(int i=0;i<jArray.length();i++){
                                    JSONObject main = jArray.getJSONObject(i);

                                    namelist.add(main.getString("name"));
                                    idlist.add(main.getString("id"));

                                    String[] spinnerArray = new String[idlist.size()];
                                    spinnerMap3 = new HashMap<>();
                                    for (int j = 0; j < idlist.size(); j++)
                                    {
                                        spinnerMap3.put(namelist.get(j),idlist.get(j));
                                        spinnerArray[j] = namelist.get(j);
                                    }

                                    adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_spinner_item, spinnerArray);
                                    adapter.setDropDownViewResource(R.layout.spinnerlayout);
                                    city_spin.setAdapter(adapter);
                                }
                            }






                        } catch (JSONException e) {
                            // JSON error
                            e.printStackTrace();
                            Toast.makeText(MainActivity.this, "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(MainActivity.this, "VolleyError" + error.toString(), Toast.LENGTH_LONG).show();

            }
        }) {
            /*@Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();


                return params;
            }*/

        };

        RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
        requestQueue.add(stringRequest);


    }




}

1 个答案:

答案 0 :(得分:0)

我最近在我的项目中实现了这一点。在填写SELECT LEFT(s.artikkelId, 2) as lev_id, *, SUM(s.pris*s.antall) AS total, SUM(s.antall) AS antall FROM ecs_statistikk s JOIN butikk_ordre bo ON s.ordreId = bo.ecs_ordre_id AND s.butikkNr = bo.site_id WHERE s.salgsDato >= '2016-06-01' AND s.salgsDato <= '2016-09-30' GROUP BY LEFT(s.artikkelId, 2) ORDER BY total DESC ; 通行证&#34; 选择项目&#34;之前,您需要做的就是在arraylist的 0 arraylist,然后用数据填充它。然后在你的Spinner适配器中覆盖以下方法 -

position

您可以看到示例here