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.onO`enter code here`ptionsItemSelected(item);
}
}
当自动完成文本框中的文本更改时,我想更新我的下拉列表。
当用户键入任何文本并且如果任何单词与字符串匹配时,我已经编写了用于更新列表的逻辑,那么它应该更新我的列表。
我在控制台中获取了正确的列表,但无法刷新列表。
我认为我的
notifyDataSetChanged()
无法正常工作。我也尝试使用
adapter.getFilter().filter(s);
而不是adapter.notifyDataSetChanged()
,但仍然遇到问题 最佳答案
由于这是autoCompleteTextView,因此您应该制作新的适配器并覆盖getFilter()方法。根据您的要求尝试此代码。
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;
}
}
这应该是您的适配器:
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();
}
}
}
};
}