0

I want to show all the suggestions to the user when the textfield is empty, I am getting my suggestions from json response, when user types the word it appears in dropdown but when the textfield is empty it doesn't show anything i even tried showDropDown() but the Android Monitor says: Attempted to finish an input event but the input event receiver has already been disposed.

Here is my code:

public class MainActivity extends AppCompatActivity {
AutoCompleteTextView acTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    acTextView = (AutoCompleteTextView) findViewById(R.id.autoComplete);
    acTextView.setAdapter(new SuggestionAdapter(this,acTextView.getText().toString()));
    acTextView.setThreshold(0);

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

i get my suggestion adapter from these classes first JSON Parser which parse the keywords from URL. A getter setter class. Suggestions Adapter which sets the filtered items in adapter. My JSON response:

 [{"lookupValueId":350,"lookupTypeId":33,"lookupValue":"Java"},{"lookupValueId":351,"lookupTypeId":33,"lookupValue":"C++"},{"lookupValueId":352,"lookupTypeId":33,"lookupValue":"Photoshop"},{"lookupValueId":353,"lookupTypeId":33,"lookupValue":"Java Script"}]

JSON Parser Class:

public class JsonParse {


public List<SuggestGetSet> getParseJsonWCF(String sName)
{
    List<SuggestGetSet> ListData = new ArrayList<SuggestGetSet>();
    try {
        String temp = sName.replace(" ", "%20");
        URL js = new URL("http://SomeUrl" + temp + "%22%7D%7D");
                    URLConnection jc = js.openConnection();
        BufferedReader reader = new BufferedReader(new InputStreamReader(jc.getInputStream()));
        String line = reader.readLine();
        JSONArray jsonResponse = new JSONArray(line);
        System.out.print("DATA: " + line);
        for(int i = 0; i < jsonResponse.length(); i++){
            JSONObject r = jsonResponse.getJSONObject(i);
            ListData.add(new SuggestGetSet(r.getInt("lookupValueId"),r.getString("lookupValue")));
        }
    } catch (Exception e1) {
        e1.printStackTrace();
    }
    return ListData;

   }
}

Getter setter class:

public class SuggestGetSet {
String name;
static Integer id;
public SuggestGetSet(int id, String name){
    this.setId(id);
    this.setName(name);
}
public static Integer getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
  }
}

And finally the Adapter class:

class SuggestionAdapter extends ArrayAdapter<String> {

private List<String> suggestions;

SuggestionAdapter(Activity context,String nameFilter) {
    super(context, android.R.layout.simple_dropdown_item_1line);
    suggestions = new ArrayList<>();
}


@Override
public int getCount() {
    return suggestions.size();
}

@Override
public String getItem(int index) {
    return suggestions.get(index);
}

@NonNull
@Override
public Filter getFilter() {
    return new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults filterResults = new FilterResults();
            JsonParse jp = new JsonParse();
            if (constraint != null) {
                // A class that queries a web API, parses the data and
                // returns an ArrayList<GoEuroGetSet>
                List<SuggestGetSet> new_suggestions = jp.getParseJsonWCF(constraint.toString());
                suggestions.clear();
                for (int i = 0; i < new_suggestions.size(); i++) {
                    suggestions.add(new_suggestions.get(i).getName());
                }

                // Now assign the values and count to the FilterResults
                // object
                filterResults.values = suggestions;
                filterResults.count = suggestions.size();
            }
            return filterResults;
        }

        @Override
        protected void publishResults(CharSequence constraint,
                                      FilterResults results) {
            if (results != null && results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }
    };
  }
}

I followed this referrence: http://manishkpr.webheavens.com/android-autocompletetextview-example-json/

KhanStan99
  • 414
  • 8
  • 20
  • Follow this thread : [Show suggestions when no text](https://stackoverflow.com/questions/2126717/android-autocompletetextview-show-suggestions-when-no-text-entered) – N.Moudgil Sep 29 '17 at 10:33
  • i went through that and many other tutorials and stackoverflow answers. Didnt found anything helpfull so finally posted a question. – KhanStan99 Sep 29 '17 at 10:40

2 Answers2

2

Get the response first and pass arraylist from activity to your adapter. your code

acTextView.setAdapter(new SuggestionAdapter(this,yourarraylist));
Geeta Gupta
  • 1,622
  • 11
  • 17
  • Currently i am doing like this: `acTextView.setAdapter(new SuggestionAdapter(this,acTextView.getText().toString()));` this will pass my text and get suggestions. what exactly you want me to do? can you please elaborate? – KhanStan99 Sep 29 '17 at 11:00
  • Nope not working. What i want to do is when user clicks the autocompletetextview it should show dropdown with all suggestions. \n This is how i am doing acTextView.setOnClickListener(new View.OnClickListener() { (@) Override public void onClick(View view) { acTextView.getHandler().post(new Runnable() { @Override public void run() { acTextView.showDropDown(); } }); } }); – KhanStan99 Sep 29 '17 at 11:04
  • just use below code autoTextView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { autoTextView.showDropDown(); } }); – Geeta Gupta Sep 29 '17 at 11:15
  • As i have already mentioned in the question that i tried showDropDown but it doesn't worked for me. – KhanStan99 Sep 29 '17 at 11:17
  • When i click on autocompletetextview it says this: Attempted to finish an input event but the input event receiver has already been disposed. – KhanStan99 Sep 29 '17 at 11:18
  • is your data is local ? or are you getting from any API? i am getting my suggestions from json response. – KhanStan99 Sep 29 '17 at 11:19
  • I already posted my code with my question. Please go through it once. – KhanStan99 Sep 29 '17 at 11:19
  • i am also getting from api – Geeta Gupta Sep 29 '17 at 11:20
  • Dont call api in adapter – Geeta Gupta Sep 29 '17 at 11:20
  • even Dont send text to adapter, you can do it from activity class – Geeta Gupta Sep 29 '17 at 11:20
  • just do this: Get the data from server and add into arrayList and then ArrayAdapter arrayAdapter = new ArrayAdapter<>(AllState.this,android.R.layout.select_dialog_item, stringlist); autoTextView.setThreshold(0); autoTextView.setAdapter(arrayAdapter); – Geeta Gupta Sep 29 '17 at 11:26
  • Looks like this will work, but i am beginner so can you please show me the full code? you can send me that in chat. – KhanStan99 Sep 29 '17 at 11:29
  • yes i am trying but if you provide the code i think it will help me a lot. Thanks – KhanStan99 Sep 29 '17 at 11:32
  • 1. ArrayList stringlist = new ArrayList<>(); – Geeta Gupta Sep 29 '17 at 11:34
  • Thanks @Geeta it helped me a lot. – KhanStan99 Sep 29 '17 at 12:45
0

First of all why do you need custom adapter if android provide you the component the same

Follow this tutorial and this

suggestion as geeta said firstly create ArrayList from JSON than pass that to adpter which is best approach

hope this help you good luck

Community
  • 1
  • 1
Sushant Gosavi
  • 3,647
  • 3
  • 35
  • 55