AutoCompleteTextView建议未显示

时间:2016-10-11 10:02:08

标签: android sql autocompletetextview

我想从SQL中显示建议AutocompleteTextView。我调试我的代码,我看到当我在autocompleteTextView中写任何字符时,然后在nMAcDetailID和cMachineName中输入值。现在我想显示建议清单。我怎样才能做到这一点?

SimpleAdapter ADAhere;

List<Map<String, String>> data = null;

txtMachineName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}

            private Timer timer=new Timer();
            private final long DELAY = 300; // milliseconds
            @Override
            public void afterTextChanged(Editable s) {
                timer.cancel();
                timer = new Timer();
                timer.schedule(
                        new TimerTask() {
                            @Override
                            public void run() {
                                // TODO: do what you need here (refresh list)
                                // you will probably need to use runOnUiThread(Runnable action) for some specific actions
                                new async_fillMachineName().execute();
                            }
                        },
                        DELAY
                );


            }
        });


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

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

            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 {
                    @SuppressWarnings("WrongThread") String query = "exec App_ac_MacName "+nClintID+",'"+txtMachineName.getText()+"'";
                    ResultSet rs = dbHelp.executeRS(query);
                    data = new ArrayList<Map<String, String>>();
                    try {
                        while (rs.next()) {
                            Map<String, String> datanum = new HashMap<String, String>();
                            datanum.put("ID",rs.getString("nMAcDetailID"));
                            datanum.put("NAME", rs.getString("cMachineName"));
                            data.add(datanum);
                            String str;
                            str = "1";
                        }

                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return resultClientID;
        }
        @Override
        protected void onPostExecute(String result1) {

            try {
                String[] fromwhere = {"ID","NAME"};
                int[] viewswhere = {R.id.nSerialMachineID,R.id.cCodeMachineNAme};
                ADAhere = new SimpleAdapter(getActivity(), data, R.layout.machine_autocomplete_list_template, fromwhere, viewswhere);
                //SimpleCursorAdapter a = new SimpleCursorAdapter(getActivity(), R.layout.machine_autocomplete_list_template, null, fromwhere, viewswhere, 0);
                //a.setStringConversionColumn(1);
                txtMachineName.setAdapter(ADAhere);

            } catch (Exception e) {

            }
            super.onPostExecute(result1);

        }
    }

1 个答案:

答案 0 :(得分:2)

如果AutocompleteTextView已使用值设置适配器,则可以使用方法showDropDown()强制视图显示建议。