skip to Main Content

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/

2

Answers


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

    acTextView.setAdapter(new SuggestionAdapter(this,yourarraylist));
    
    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search