1
public class MainActivity extends ActionBarActivity {



AutoCompleteTextView autoCompleteTextView;
String[] list;
int textlength = 0;
EditText edt;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    list = getResources().getStringArray(R.array.month);



    final ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
            android.R.layout.simple_dropdown_item_1line, list);
    autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.tv);

    autoCompleteTextView.setAdapter(adapter);

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

    autoCompleteTextView.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            textlength = autoCompleteTextView.getText().length();

            String searchString = autoCompleteTextView.getText().toString();
            for (int i = 0; i < list.length; i++) {

                String str = list[i].toLowerCase();

                if (str.contains(searchString)) {
                    System.out.println("List matched items " + list[i]);
                }
                adapter.notifyDataSetChanged();
            }

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_visit_repo) {
        Uri uri = Uri.parse("https://github.com/Lesilva/BetterSpinner");
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intent);


        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

I want to update my dropdown list when text changes in autocomplete text box. I have written logic for updating the list when the user types any text and if any word matches to the string then it should update my list. I am getting the correct list in the console but unable to refresh list.

I think my notifyDataSetChanged() is not working properly.

I have also tried adapter.getFilter().filter(s); instead of adapter.notifyDataSetChanged() but still getting problem

grrigore
  • 1,050
  • 1
  • 21
  • 39
  • when string matches to list item .. you are printing on console, where is your code for updating list ? only one line is there i.e. System.out.println("List matched items " + list[i]); you are not changing anything in your adapter .. – Surabhi Singh Jan 25 '16 at 06:53
  • I have written adapter.notifyDataSetChanged(); ..??How to update it....I am new in android and know only this way to update or refresh list – TechnologyLearner Jan 25 '16 at 06:56
  • but where you updated your list ?? list is same according to me ? – Surabhi Singh Jan 25 '16 at 06:58
  • what is your requirement ? what updation you want while text change in autocompletetextview.. I tried your code and it is working fine as written, but if you want any updation you have to write that .. – Surabhi Singh Jan 25 '16 at 07:04
  • share your listview adapter code – HourGlass Jan 25 '16 at 07:04
  • @SurabhiSingh Suppose i type 'ry' then list should show all string having words 'ry' anywhere in the list. Example... if i type 'ry' then it should show january,february – TechnologyLearner Jan 25 '16 at 07:10
  • ok !! I checked your edited comment now.. i will post code for your requirement !! – Surabhi Singh Jan 25 '16 at 07:27

3 Answers3

1

As this is autoCompleteTextView, You should make new adapter and overwrite getFilter() method. Try this code for your requirement.

public class MainActivity extends AppCompatActivity {

AutoCompleteTextView autoCompleteTextView;
String[] list;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    list = getResources().getStringArray(R.array.month);

    final List<String> arrList = new ArrayList<>();
    Collections.addAll(arrList, list);




    final CustomAdapter adapter = new CustomAdapter(this,android.R.layout.simple_dropdown_item_1line,arrList,list);
    autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.tv);

    autoCompleteTextView.setAdapter(adapter);

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


}




@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}


}

and this should be your adapter:

public class CustomAdapter extends ArrayAdapter<String> {
private String[] list;




public CustomAdapter(Context context, int resource, List<String> objects, String[] list) {
    super(context, resource, objects);
    this.list = list;
}



@Override
public android.widget.Filter getFilter() {
    return filter;
}
android.widget.Filter filter = new android.widget.Filter() {
    @Override
    protected FilterResults performFiltering(CharSequence charSequence) {
        if(charSequence != null) {


            String searchString = charSequence.toString();
            List<String> newArrList = new ArrayList<>();
            FilterResults filterResults = new FilterResults();

            for (String aList : list) {

                String str = aList.toLowerCase();


                if (str.contains(searchString)) {
                    System.out.println("List matched items " + aList);
                    newArrList.add(aList);
                }


            }
            filterResults.values = newArrList;
            filterResults.count = newArrList.size();
            return filterResults;

        }else {

            return new FilterResults();
        }
    }

    @Override
    protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
        if(filterResults != null ) {
            ArrayList<String> filteredList = (ArrayList<String>) filterResults.values;
            if (filterResults.count > 0) {
                clear();

                for (int i = 0; i < filteredList.size(); i++) {
                    add(filteredList.get(i));
                }
                notifyDataSetChanged();
            }
        }

    }
};
}
Surabhi Singh
  • 803
  • 8
  • 14
0

what you want the dropdown list to display is whether the matching result or not, if so, you should change the list associated with the adapter first. Such as:

...

if (str.contains(searchString)) {
    list.clear();
    list.add("" + str);
    adapter.notifyDataSetChanged();
} 

...
linjiang
  • 116
  • 10
0

You should update your list data when textChange. Try something like this:

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    textlength = autoCompleteTextView.getText().length();
    String searchString = autoCompleteTextView.getText().toString();

    List<String> updatedArr = new ArrayList<String>;

    for (int i = 0; i < list.length; i++) {
        String str = list[i].toLowerCase();

        if (str.contains(searchString)) {
            System.out.println("List matched items " + list[i]);
            updatedArr.add(list[i]);
        }

    }
    list = updatedArr.toArray(new String[updatedArr.size()]);
    adapter.clear();
    adapter.addAll(list);
    adapter.notifyDataSetChange();
}

Do let me know if this is working. Hope this helps!

EDIT

Try changing your list:

List<String> list = new ArrayList<>;

So in the onTextChanged function, you can easily do:

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    textlength = autoCompleteTextView.getText().length();
    String searchString = autoCompleteTextView.getText().toString();

    List<String> updatedList = new ArrayList<String>;

    for (String item : list) {
        String str = item.toLowerCase();

        if (str.contains(searchString)) {
            System.out.println("List matched items " + item);
            updatedList.add(item);
        }

    }
    list.clear();
    list.addAll(updatedList);
    adapter.notifyDataSetChange();
}

Much simpler, try ;)

Jiyeh
  • 5,187
  • 2
  • 30
  • 31
  • Not working..app crashed at adapter.clear(); .... java.lang.UnsupportedOperationException – TechnologyLearner Jan 25 '16 at 07:38
  • I think it's bacause of String[] array, can you replace it with List instead? http://stackoverflow.com/questions/3200551/unable-to-modify-arrayadapter-in-listview-unsupportedoperationexception – Jiyeh Jan 25 '16 at 07:55
  • It will not work !! same way I also tried .. As this is autoCompleteTextView It is already applying some filter but if you want to change that filter result your have to overwrite getFilter() method and should provide your logic for filtering !! – Surabhi Singh Jan 25 '16 at 09:38