4

I am trying to use the autocompletetextview in Android Studio to provide suggestions for every letter keyed-in by the user.

Every time a letter is keyed-in, an API call is made like this,

http://dev.markitondemand.com/MODApis/Api/v2/Lookup/json?input=app
http://dev.markitondemand.com/MODApis/Api/v2/Lookup/json?input=appl
http://dev.markitondemand.com/MODApis/Api/v2/Lookup/json?input=apple

The JSON array that's returned from the API call is populated in the suggestions list-box.

So far I got the activity_main.xml file like this,

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.raam.stockmarketviewer.MainActivity">

    <AutoCompleteTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/stocks"
        android:hint="@string/hint" />
</RelativeLayout>

After this how should I structure the MainActivity.java file to accomplish the auto-suggestions feature?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Kemat Rochi
  • 932
  • 3
  • 21
  • 35
  • just follow [this](http://stackoverflow.com/a/19860624/2252830) code snippet – pskink Apr 22 '16 at 17:35
  • 1
    and if you have no idea how to change the json part of the code i posted above, [this](http://pastebin.com/VUm8P4mb) is a working solution for your case, just copy/paste it in your `Activity#onCreate` method – pskink Apr 23 '16 at 04:52
  • @pskink Thank you sooooo much for the working solution. Its working awesome. I just started learning Android Development a few days back and have to complete an App in a week for a school assignemnt. Sorry if I'm posting silly questions – Kemat Rochi Apr 23 '16 at 08:58
  • @pskink Could you let me know how do we include the "Exchange" information also from the JSON array in the suggestions listbox? – Kemat Rochi Apr 23 '16 at 10:35
  • the same way as "Name" was read – pskink Apr 23 '16 at 10:40
  • @pskink - I tried something like this - http://pastebin.com/CumY1iNB but the "Name" got replaced by "Exchange". What did I do wrong? :( – Kemat Rochi Apr 23 '16 at 11:28
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/109996/discussion-between-kemat-rochi-and-pskink). – Kemat Rochi Apr 23 '16 at 11:30
  • becase you use the same textview in `int[] to` R.id.text2 is used twice – pskink Apr 23 '16 at 11:34
  • @pskink Then could you tell me how would be the right approach please? – Kemat Rochi Apr 23 '16 at 12:20
  • if you want three different fields to be shown you should have a layout file with 3 TextViews, simple_list_item_2.xm layout file has only 2 such fields – pskink Apr 23 '16 at 12:22

1 Answers1

8

Just create a simple adapter and update it every time you get results

  List<String> suggestions = new ArrayList<>();
  ArrayAdapter<String> adapter ;
   .
   .
   .
   // in your onCreate

   autocomplete = (AutoCompleteTextView)findViewById(R.id.stocks);
   adapter = new ArrayAdapter<>(this,
            android.R.layout.simple_dropdown_item_1line, suggestions);
   autocomplete.setAdapter(arrayAdapter);

   autocomplete.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) {
          //retrieveData(s);
        }

        @Override
        public void afterTextChanged(Editable s) {
            retrieveData(s); //this will call your method every time the user stops typing, if you want to call it for each letter, call it in onTextChanged 

        }
    });
   .
   .
   .
   // where you get the data, I suppose in a list
   private void retrieveData(String s){
    //Do your stuff here with the String s and store the list of your results in the list suggestions
   suggestions = yourList;
   adapter.notifyDataSetChanged();

   }
David Seroussi
  • 1,650
  • 2
  • 17
  • 34
  • A handler could also be used to trigger the retrieveData method here. It would help in maintaining the threshold. As shown here: https://www.truiton.com/2018/06/android-autocompletetextview-suggestions-from-webservice-call/ – KnowIT Jun 23 '18 at 05:52
  • Sorry for digging this up again.. How are you referencing the adapter inside the retrieveData method? As that method is created outside of the onCreate method, do we pass the adapter in the method call? – Daniel Lawton Apr 12 '20 at 11:16
  • The adapter is a global variable that I initialize in the onCreate, so that it can be used later by the retrieveData method – David Seroussi Apr 12 '20 at 23:58
  • Please see another answer here : https://stackoverflow.com/a/68313126/3904109 – DragonFire Jul 09 '21 at 07:53