Smith Gta
Smith Gta

Reputation: 7

Add items to listview dynamically from autocompletetextview

public void AutocmpleteMeth() {
    // Hieronder is het code voor Autocomplete [BEGIN]
    final AutoSuggestAdapter adapter = new AutoSuggestAdapter(this, android.R.layout.simple_dropdown_item_1line, lstProduct);
    ACTV.setAdapter(adapter);
    ACTV.setThreshold(1);

    ACTV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

            String selectedItem = (String) arg0.getItemAtPosition(position);
            Boodschappenlst.add(selectedItem);
            ACTV.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
                        Log.e("TAG", "Done pressed");
                        ACTV.setText("");

                    }
                    return false;
                }
            });
        }
        //Autocmplete [END]
    });

}

This my autocomplete method that is working perfect to add an item to the listview by pressing on done key and after the key is pressed keyboard disappear and autocomplete text get set to ''. I want to make it more dynamic. Like what now happens is when a user click on an item in suggestionslist. It comes to the Autocompletetextview field and after that user have to press done on keyboard and it showed up to the listview. But what i want is that it directly adds to the listview. On the moment when user click on any item in suggestion list it dont go to textfield and user dont have to press done. It just add that item to listview on the click and textfield get reset. Thanks in advance.

Upvotes: 0

Views: 1179

Answers (1)

Veneet Reddy
Veneet Reddy

Reputation: 2937

You need to call notifyDataSetChanged() on your related ListView's adapter after adding item to Boodschappenlst. This method should do what you need:

public void AutocmpleteMeth() {
 // Hieronder is het code voor Autocomplete [BEGIN]
 final AutoSuggestAdapter adapter = new AutoSuggestAdapter(this, android.R.layout.simple_dropdown_item_1line, lstProduct);
 ACTV.setAdapter(adapter);
 ACTV.setThreshold(1);

 ACTV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  @Override
  public void onItemClick(AdapterView < ? > arg0, View arg1, int position, long arg3) {

    String selectedItem = (String) arg0.getItemAtPosition(position);
    Boodschappenlst.add(selectedItem);
    // Call notifyDatasetChanged() here on the related ListView's adapter here to recognise new item change
    ACTV.setText("");
   }
   //Autocmplete [END]
 });
}

Upvotes: 1

Related Questions