我正在尝试根据另一个 View (微调器)中选择的选项来更改 AutoCompleteTextView 的建议列表。不幸的是,这个 TextView 的适配器似乎没有从另一个 View 的 setOnClickListener 方法中得到通知。我认为这可能是由于上下文的差异而发生的?如果是这样,我该如何解决它?
public class AutoCompleteGOTPointAdapter extends ArrayAdapter<GOTPoint> {
Context mContext;
List<GOTPoint> GOTPointListAll;
public AutoCompleteGOTPointAdapter(@NonNull Context context, @NonNull List<GOTPoint> GOTPointList) {
super(context, 0, GOTPointList);
this.mContext = context;
this.GOTPointListAll = new ArrayList<>(GOTPointList);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.one_line_spinner,parent,false);
}
TextView textViewGOTPointName = convertView.findViewById(R.id.my_spinner);
GOTPoint GOTPoint = this.getItem(position);
if (GOTPoint != null) {
textViewGOTPointName.setText(GOTPoint.getName());
}
return convertView;
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
public String convertResultToString(Object resultValue) {
return ((GOTPoint) resultValue).getName();
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
List<GOTPoint> suggestion = new ArrayList<>();
if (constraint == null || constraint.length() == 0) { //
suggestion.addAll(GOTPointListAll);
}
else {
String filterPattern = constraint.toString().toLowerCase().trim();
for (GOTPoint gotPoint : GOTPointListAll) {
if (gotPoint.getName().toLowerCase().contains(filterPattern)) {
suggestion.add(gotPoint);
}
}
}
filterResults.values = suggestion;
filterResults.count = suggestion.size();
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
clear();
addAll((List) results.values);
notifyDataSetChanged();
}
};
}
}
public class SearchActivity extends AppCompatActivity {
Spinner spnMountainChains;
AutoCompleteTextView edtEndPoint;
AutoCompleteGOTPointAdapter GOTPointAdapter;
List<GOTPoint> GOTPoints;
@Override
protected void onCreate(Bundle savedInstanceState) {
spnMountainChains = findViewById(R.id.spn_mountain_chains);
edtEndPoint = findViewById(R.id.actv_end_point);
GOTPoints = new ArrayList<GOTPoint>();
GOTPointAdapter = new AutoCompleteGOTPointAdapter( SearchActivity.this,GOTPoints);
edtEndPoint.setAdapter(GOTPointAdapter);
spnMountainChains.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
MountainChain mountainChain = (MountainChain) spnMountainChains.getSelectedItem();
List<GOTPoint> filteredGOTPoints = dao.getGOTPoints(mountainChain.getId());
GOTPointAdapter.clear();
GOTPointAdapter.addAll(filteredGOTPoints);
// GOTPoints.clear(); // Also doesn't work
// GOTPoints.addAll(filteredGOTPoints);
// GotPointAdapter.notifyDataSetChanged();
}
}
}
one_line_spinner.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:textSize="21sp">
</TextView>
最佳答案
好的,感谢@MikeM。评论 我设法找到了解决方案。问题确实是初始化后没有更改 GOTPointsListAll
。要在从微调器中选择一个项目时更改它,我必须做几件事:
向适配器类添加
GOTPointList
属性并在构造函数中初始化它:Context mContext; List<GOTPoint> GOTPointListAll; List<GOTPoint> GOTPointList; public AutoCompleteGOTPointAdapter(@NonNull Context context, @NonNull List<GOTPoint> GOTPointList) { super(context, 0, GOTPointList); this.mContext = context; this.GOTPointList = GOTPointLi this.GOTPointListAll = new ArrayList<>(GOTPointList); }
- 覆盖我的适配器的
notifyDataSetChanged
方法:
@Override public void notifyDataSetChanged() { GOTPointListAll.clear(); GOTPointListAll.addAll(GOTPointList); notifyDataSetChangedSuper(); }
- 覆盖我的适配器的
哪里
notifyDataSetChangedSuper
是:private void notifyDataSetChangedSuper() { super.notifyDataSetChanged(); }
我可能应该将对 GOTPoinListAll
的操作放到另一个函数中,而不是覆盖现有函数,但这是一个细节。拥有这两个函数的原因是,仅在选择微调项目时才需要更新 GOTPointListAll
,并且 GOTPointList
需要更新<过滤完成后,始终。这就是为什么:
- 在
publishResults()
部分:
clear();
addAll((List) results.values);
notifyDataSetChangedSuper();
应更改为:
GOTPointList.clear();
GOTPointList.addAll((List) results.values);
notifyDataSetChangedSuper();
- 最后在
onItemSelected()
中:
GOTPoints.clear();
GOTPoints.addAll(filteredGOTPoints);
GOTPointAdapter.notifyDataSetChanged();
感谢您的帮助!
关于java - AutoCompleteTextView 建议无法更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61721036/