如何在hashmap中的微调器中的零位置添加项目?

时间:2016-10-17 08:05:45

标签: android hashmap spinner

我使用下面的代码从SQL填充spinner的hashmap。现在我想添加硬编码" select"在零位置,但我失败了。我使用了resultListMachineClientID .add(),但它不允许我添加值。我怎样才能做到这一点?

ArrayList<HashMap<String, String>> resultListMachineClientID = new ArrayList<>();

class async_fillClientId extends AsyncTask<String, Void, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            showDialog();
        }

        @Override
        protected String doInBackground(String... params) {

            resultListMachineClientID.clear();
            try {
                Connection con = connectionClass.CONN();
                if (con == null) {

                    getActivity().getParent().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getActivity(), "please Check Your Internet Connection", Toast.LENGTH_LONG).show();
                        }
                    });
                } else {
                    String query = "exec App_Combo_ClientDetail";
                    ResultSet rs = dbHelp.executeRS(query);
                    try {
                        while (rs.next()) {
                            HashMap<String, String> hashstate = new HashMap<String, String>();
                            hashstate.put(TAG_ID_machineClientId, rs.getString("nClientID"));
                            hashstate.put(TAG_Name_machineClientName, rs.getString("cClientName"));
                            resultListMachineClientID.add(hashstate);
                        }
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return resultClientID;
        }
        @Override
        protected void onPostExecute(String result1) {

            try {

                // TODO Auto-generated method stub
                adapterClientID = new SimpleAdapter(getActivity(), resultListMachineClientID, R.layout.activity_showspinner_client_id, new String[]{TAG_ID_machineClientId, TAG_Name_machineClientName}, new int[]{R.id.nSerialClientID, R.id.cCodeClientNAme});
                spnClientID.setAdapter(adapterClientID);
                hideDialog();

                spnClientID.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view,
                                               int pos, long id) {
                        // TODO Auto-generated method stub

                        TextView txtid1 = (TextView) parent.findViewById(R.id.nSerialClientID);

                        nClintID = txtid1.getText().toString();
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> arg0) {
                        // TODO Auto-generated method stub

                    }
                });

            } catch (Exception e) {

            }
            super.onPostExecute(result1);

        }
    }

2 个答案:

答案 0 :(得分:0)

在此之前添加

hashstate.put(TAG_Name_machineClientName, "Select Value");

之后输入服务器数据

 String query = "exec App_Combo_ClientDetail";
                    ResultSet rs = dbHelp.executeRS(query);
                    try {
                        while (rs.next()) {
                            HashMap<String, String> hashstate = new HashMap<String, String>();
                            hashstate.put(TAG_ID_machineClientId, rs.getString("nClientID"));
                            hashstate.put(TAG_Name_machineClientName, rs.getString("cClientName"));
                            resultListMachineClientID.add(hashstate);
                        }
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }

答案 1 :(得分:0)

试试这个..

@Override
        protected String doInBackground(String... params) {

            resultListMachineClientID.clear();
// add these lines
                HashMap select= new HashMap();
                select.put("select","select");
                resultListMachineClientID .add(select);
//