1

I want to show suggestions AutocompleteTextView from SQL. I debug my code and I see that when I write any character in autocompleteTextView then value coming in nMAcDetailID and cMachineName. Now i want to show suggestion list. How can I achieve this ?

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);

        }
    }
  • see my answer [here](http://stackoverflow.com/a/19860624/2252830) – pskink Oct 11 '16 at 11:27
  • Thanks pskink. When I Select any value from AutocompleteTextView then its showing {NAME =ASJ 111,ID=106005} in my AutocompleteTextView. I want to show only "ASJ 111". –  Oct 13 '16 at 11:14
  • did you try the link i posted? – pskink Oct 13 '16 at 11:19
  • Ya I tried your link and its showing suggestions in autocomplete. when I click on any suggestion then I got result in my aucompletetextview like this {NAME =ASJ 111,ID=106005} I want to show only value of name. –  Oct 13 '16 at 11:26
  • Above code in my ques was successfully showing suggestion –  Oct 13 '16 at 11:45
  • so run my code without any changes, type `rahul`, select some item from the list and see what happens – pskink Oct 13 '16 at 12:17
  • I have not used provider from your code. –  Oct 13 '16 at 12:42
  • so what happens if you select some item using my code? – pskink Oct 13 '16 at 12:51
  • When I am typing any word in autocompletetextview the suggestion list was showing perfect. But when I select some item from list its showing both Name and Id like above in autocompletetextview . I want to show name only in autocompletetextview like showing in list. –  Oct 13 '16 at 13:40
  • did you run **my** code? without any changes? just copy/paste it to your Activity#onCreate, type something and select the item – pskink Oct 13 '16 at 13:47

1 Answers1

2

If AutocompleteTextView has set adapter with values you can use method showDropDown() to force view to show suggestions.

Thror
  • 437
  • 4
  • 4
  • When I Select any value from AutocompleteTextView then its showing {NAME =ASJ 111,ID=106005} in my AutocompleteTextView. I want to show only "ASJ 111" –  Oct 13 '16 at 11:14