我想制作相同的可搜索编辑文本,其中包含在谷歌地图中实施的建议列表,因为我使用的是自动完成TextView,但问题是我无法使用更多回收站视图实现自定义建议列表而不仅仅是一个文本字符串,但当我设置一个内置的数组适配器它工作得很好,任何帮助将不胜感激。
public void attachingListeners(final ActivityMainBinding mainBinding){
mainBinding.searchPlaces.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String location = mainBinding.searchPlaces.getText().toString();
Geocoder geocoder = new Geocoder(getBaseContext());
List<Address> addresses = null;
try {
// Getting a maximum of 3 Address that matches the input
// text
addresses = geocoder.getFromLocationName(location, 6);
for (Address address:
addresses) {
autoTextList.add(new AutoText(address.getFeatureName().toString()));
}
/*adapter = new ArrayAdapter<>
(MainActivity.this, android.R.layout.simple_list_item_1, autoTextList);*/ // this works
autoAdapter = new AutoCompleteAdapter(autoTextList, MainActivity.this);
//dashboardBinding.dashboardList.setAdapter(dashboardGirdAdapter);
mainBinding.searchPlaces.setThreshold(1);
mainBinding.searchPlaces.setAdapter(autoAdapter); //<-- error
if (addresses != null && !addresses.equals(""))
search(addresses);
} catch (Exception e) {
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
我的RecyclerView适配器:
public class AutoCompleteAdapter extends RecyclerView.Adapter<AutoCompleteAdapter.AutoCompleteHolder>{
private List<AutoText> autoList;
private LayoutInflater inflater;
private AutoCompleteItemBinding itemBinding;
public AutoCompleteAdapter(List<AutoText> autoList, Context context) {
this.autoList = autoList;
this.inflater = LayoutInflater.from(context);
}
@Override
public AutoCompleteHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.auto_complete_item, parent, false);
return new AutoCompleteHolder(view);
}
@Override
public void onBindViewHolder(AutoCompleteHolder holder, int position) {
AutoText autoText = autoList.get(position);
itemBinding.autoItem.setText(autoText.getAutoString());
}
@Override
public int getItemCount() {
return autoList.size();
}
class AutoCompleteHolder extends RecyclerView.ViewHolder{
public AutoCompleteHolder(View itemView) {
super(itemView);
itemBinding = AutoCompleteItemBinding.bind(itemView);
}
}
}