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