簡體   English   中英

Autocompletetextview不顯示帶有自定義適配器的下拉列表

[英]Autocompletetextview not showing dropdown with custom adapter

我有一個autocompletetextview。 我從API獲取結果,並在textchanged上發送給適配器。

這是適配器。

public class ProductSearchAdapter extends BaseAdapter implements Filterable {

    private Context context;
    private ArrayList<ProductListModel> originalList;
    private ArrayList<ProductListModel> suggestions = new ArrayList<>();
    private Filter filter = new CustomFilter();


    public ProductSearchAdapter(Context context, ArrayList<ProductListModel> originalList) {
        this.context = context;
        this.originalList = originalList;
    }

    @Override
    public int getCount() {
        return suggestions.size(); // Return the size of the suggestions list.
    }

    @Override
    public Object getItem(int position) {
        return originalList.get(position).getName();
    }


    @Override
    public long getItemId(int position) {
        return 0;
    }

    /**
     * This is where you inflate the layout and also where you set what you want to display.
     * Here we also implement a View Holder in order to recycle the views.
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(context);

        ViewHolder holder;

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.product_search_row, parent, false);
            holder = new ViewHolder();

            holder.textViewProductName = (TextView) convertView.findViewById(R.id.textViewProductName);
            holder.imageViewProductImage = (ImageView) convertView.findViewById(R.id.imageViewProductImage);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.textViewProductName.setText(originalList.get(position).getName());
        Picasso.with(context)
                .load(originalList.get(position).getImagesSmall().get(0).getSrc())
                .into(holder.imageViewProductImage);


        return convertView;
    }


    @Override
    public Filter getFilter() {
        return filter;
    }

    private static class ViewHolder {
        ImageView imageViewProductImage;
        TextView textViewProductName;
    }

    /**
     * Our Custom Filter Class.
     */
    private class CustomFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            suggestions.clear();

            if (originalList != null && constraint != null) { // Check if the Original List and Constraint aren't null.
                for (int i = 0; i < originalList.size(); i++) {
                    if (originalList.get(i).getName().toLowerCase().contains(constraint)) { // Compare item in original list if it contains constraints.
                        suggestions.add(originalList.get(i)); // If TRUE add item in Suggestions.
                    }
                }
            }
            FilterResults results = new FilterResults(); // Create new Filter Results and return this to publishResults;
            results.values = suggestions;
            results.count = suggestions.size();

            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            if (results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }
    }
}

現在的問題是下拉列表沒有顯示。 而如果我嘗試使用與數組適配器相同的autocompletetextview,則會顯示出來。

這是我從以下位置調用api的活動部分:

autoCompleteTextViewSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (charSequence.toString().length() > 0) {
                    hitSearchAPI(charSequence.toString());
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

關於API響應:

final GsonBuilder gsonBuilder = new GsonBuilder();
        final Gson gson = gsonBuilder.create();

        productList = gson.fromJson(responseString, ProductListModel[].class);


arrayListProducts = new ArrayList<ProductListModel>(Arrays.asList(productList));


        productsSearchAdapter = new ProductSearchAdapter(MainActivity.this, arrayListProducts);
        autoCompleteTextViewSearch.setThreshold(1);
        autoCompleteTextViewSearch.setAdapter(productsSearchAdapter);

相同的textview使用數組適配器,但不適用於自定義適配器。

產品清單型號:

public class ProductListModel {
    String _id;
    String name;
    String color;
    String description;
    int credits;
    ProductItemModel category;
    ArrayList<ProductItemModel> subcategories;
    ProductItemModel fit;
    ProductBrandModel brand;
    ArrayList<ProductItemModel> rules;
    ProductBrandModel condition;
    ArrayList<ProductImagesModel> images;
    ArrayList<ProductItemModel> size;
    ArrayList<ProductImagesModel> imagesSmall;
    String userId;
    long time_created;
    long time_approved;
    long time_featured;
    long time_rejected;
    boolean approved;
    boolean rejected;
    boolean featured;
    int status;
    ProductUserProfileModel user_profile;
    String rejected_reason_id;
    String categoryId;
    int likes;
    boolean likedBy;
    public String get_id() {
        return _id;
    }
    public void set_id(String _id) {
        this._id = _id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public int getCredits() {
        return credits;
    }
    public void setCredits(int credits) {
        this.credits = credits;
    }
    public ProductItemModel getCategory() {
        return category;
    }
    public void setCategory(ProductItemModel category) {
        this.category = category;
    }
    public ArrayList<ProductItemModel> getSubcategories() {
        return subcategories;
    }
    public void setSubcategories(ArrayList<ProductItemModel> subcategories) {
        this.subcategories = subcategories;
    }
    public ProductItemModel getFit() {
        return fit;
    }
    public void setFit(ProductItemModel fit) {
        this.fit = fit;
    }
    public ProductBrandModel getBrand() {
        return brand;
    }
    public void setBrand(ProductBrandModel brand) {
        this.brand = brand;
    }
    public ArrayList<ProductItemModel> getRules() {
        return rules;
    }
    public void setRules(ArrayList<ProductItemModel> rules) {
        this.rules = rules;
    }
    public ProductBrandModel getCondition() {
        return condition;
    }
    public void setCondition(ProductBrandModel condition) {
        this.condition = condition;
    }
    public ArrayList<ProductImagesModel> getImages() {
        return images;
    }
    public void setImages(ArrayList<ProductImagesModel> images) {
        this.images = images;
    }
    public ArrayList<ProductItemModel> getSize() {
        return size;
    }
    public void setSize(ArrayList<ProductItemModel> size) {
        this.size = size;
    }
    public ArrayList<ProductImagesModel> getImagesSmall() {
        return imagesSmall;
    }
    public void setImagesSmall(ArrayList<ProductImagesModel> imagesSmall) {
        this.imagesSmall = imagesSmall;
    }
    public String getUserId() {
        return userId;
    }
    public void setUserId(String userId) {
        this.userId = userId;
    }
    public long getTime_created() {
        return time_created;
    }
    public void setTime_created(long time_created) {
        this.time_created = time_created;
    }
    public long getTime_approved() {
        return time_approved;
    }
    public void setTime_approved(long time_approved) {
        this.time_approved = time_approved;
    }
    public long getTime_featured() {
        return time_featured;
    }
    public void setTime_featured(long time_featured) {
        this.time_featured = time_featured;
    }
    public long getTime_rejected() {
        return time_rejected;
    }
    public void setTime_rejected(long time_rejected) {
        this.time_rejected = time_rejected;
    }
    public boolean isApproved() {
        return approved;
    }
    public void setApproved(boolean approved) {
        this.approved = approved;
    }
    public boolean isRejected() {
        return rejected;
    }
    public void setRejected(boolean rejected) {
        this.rejected = rejected;
    }
    public boolean isFeatured() {
        return featured;
    }
    public void setFeatured(boolean featured) {
        this.featured = featured;
    }
    public int getStatus() {
        return status;
    }
    public void setStatus(int status) {
        this.status = status;
    }
    public ProductUserProfileModel getUser_profile() {
        return user_profile;
    }
    public void setUser_profile(ProductUserProfileModel user_profile) {
        this.user_profile = user_profile;
    }
    public String getRejected_reason_id() {
        return rejected_reason_id;
    }
    public void setRejected_reason_id(String rejected_reason_id) {
        this.rejected_reason_id = rejected_reason_id;
    }
    public String getCategoryId() {
        return categoryId;
    }
    public void setCategoryId(String categoryId) {
        this.categoryId = categoryId;
    }
    public int getLikes() {
        return likes;
    }
    public void setLikes(int likes) {
        this.likes = likes;
    }
    public boolean isLikedBy() {
        return likedBy;
    }
    public void setLikedBy(boolean likedBy) {
        this.likedBy = likedBy;
    }
}

您需要向模型中添加toString()方法,以便AutoCompleteTextView可以在鍵入的String和返回的值之間進行比較。

如果按name查找,則toString()需要返回它:

 @Override
        public String toString() {
            return name ;
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM