gpt4 book ai didi

android - 下拉列表未更新 onTextChange 监听器

转载 作者:行者123 更新时间:2023-11-30 01:37:05 29 4
gpt4 key购买 nike

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);
}
}

我想在自动完成文本框中的文本更改时更新我的​​下拉列表。我已经编写了当用户键入任何文本时更新列表的逻辑,如果任何单词与字符串匹配,那么它应该更新我的列表。我在控制台中获得了正确的列表,但无法刷新列表。

我认为我的 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();
}
}

}
};
}

关于android - 下拉列表未更新 onTextChange 监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34986388/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com