我有这个 ArrayAdapter:
public class PlacesAutoCompleteAdapter extends ArrayAdapter<String> implements Filterable{
List<String> resultList;
private OnAutoCompleteListener listener;
private Context mContext;
private int mResource;
public PlacesAutoCompleteAdapter(Context context, int resource,List<String>resultList, OnAutoCompleteListener listener) {
super(context, resource);
mContext = context;
mResource = resource;
this.resultList = resultList;
this.listener = listener;
}
@Override
public int getCount() {
// Last item will be the footer
return resultList.size();
}
public void updateList(List<String>values ){
this.resultList = values;
notifyDataSetChanged();
}
@Override
public String getItem(int position) {
return resultList.get(position);
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if (constraint != null) {
List<String> resultList = listener.onAutoComplete(constraint.toString());
Log.d(getClass().getName(),String.valueOf(resultList.size()));
filterResults.values = resultList;
filterResults.count = resultList.size();
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results != null && results.count > 0) {
notifyDataSetChanged();
}
else {
notifyDataSetInvalidated();
}
}
};
return filter;
}
}
给出这个 Flowable 的结果:
void addToList(Places places){
places.getPredictions().stream().forEach(s->System.out.println(s.getDescription()));
places.getPredictions()
.stream()
.map(s->resultList.add(s.getDescription()));
}
@Override
public List<String> loadPlaces(String input) {
HappyParkApp.getInstance().createAppComponent().inject(this);
PlaceAPIService service = retrofit.create(PlaceAPIService.class);
Flowable<Places> places = service.getPlaces(Constants.APIKEY,Constants.TYPES,Constants.LANGUAGE,Constants.COMPONENTS,input);
places.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(newPlace -> addToList(newPlace));
return resultList;
}
}
这是服务:
public interface PlaceAPIService {
@GET("json")
Flowable<Places> getPlaces(@Query("key")String API,
@Query("types")String address,
@Query("language")String language,
@Query("components")String component,
@Query("input") String input);
这是正确的,我可以查看日志以了解服务是否良好。 问题是显示适配器中项目的日志始终为 0。 为什么? 我可以在哪里更新适配器?
谢谢
编辑:这是模型、 View 和演示者:
public class APIModel implements AddWithText.Model {
@Inject
Retrofit retrofit;
AddWithText.Presenter presenter;
public APIModel(AddWithText.Presenter presenter) {
this.presenter = presenter;
}
final List<String> resultList = new ArrayList<>();
void addToList(Places places){
places.getPredictions().stream().forEach(s->System.out.println(s.getDescription()));
places.getPredictions()
.stream()
.map(s->resultList.add(s.getDescription()));
}
@Override
public List<String> loadPlaces(String input) {
HappyParkApp.getInstance().createAppComponent().inject(this);
PlaceAPIService service = retrofit.create(PlaceAPIService.class);
Flowable<Places> places = service.getPlaces(Constants.APIKEY,Constants.TYPES,Constants.LANGUAGE,Constants.COMPONENTS,input);
places.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(newPlace -> addToList(newPlace));
return resultList;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_add_with_text, container, false);
ButterKnife.bind(this,rootView);
HappyParkApp.getInstance().createAppComponent().inject(this);
presenter = new AddWithTextPresenter();
adapter = new PlacesAutoCompleteAdapter(getActivity(),R.layout.list_item,autoComplete,this);
DatabaseReference root = FirebaseDatabase.getInstance().getReference();
final DatabaseReference condition = root.child("condition");
insertText.setAdapter(adapter);
return rootView;
}
@Override
public void onResume() {
super.onResume();
presenter.setView(this);
}
@Override
public List<String> onAutoComplete(String character) {
List<String> result = presenter.onTextChanged(character);
return result;
}
@Override
public void refreshList(List<String> resultList) {
adapter.updateList(resultList);
}
public class AddWithTextPresenter implements AddWithText.Presenter {
AddWithText.View view;
AddWithText.Model model;
@Inject
public AddWithTextPresenter(){
model = new APIModel(this);
}
@Override
public void onComplete(List<String> resultList) {
view.refreshList(resultList);
}
@Override
public void setView(AddWithText.View view) {
this.view = view;
}
@Override
public List<String> onTextChanged(String character) {
return model.loadPlaces(character);
}
最佳答案
你错了
adapter = new PlacesAutoCompleteAdapter(getActivity(),R.layout.list_item,autoComplete,this);
在 Adapter 中你有 4 个参数,你只传递了 3 个。
第二次检查你在这里获取数据 -
@Override
public List<String> onAutoComplete(String character) {
List<String> result = presenter.onTextChanged(character);
return result;
}
如果是这样,那么您不会在任何地方将数据传递给适配器。在适配器中添加类似 setData() 的方法,然后将数据列表传递给它。添加以下方法
public List<String> getData() {
return resultList;
}
public void setData(List<String> mData) {
this.resultList = mData;
}
public void addItems(List<String> mData) {
if (this.resultList != null) {
this.resultList.addAll(mData);
notifyItemInserted(this.resultList.size());
}
}
关于android - 阵列适配器不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43979620/