Java Autocompletetextview不显示带有自定义适配器的下拉列表
我有一个自动完成文本视图。我正在从API获取结果,并将结果发送到textchanged上的适配器 这是适配器。Java Autocompletetextview不显示带有自定义适配器的下拉列表,java,android,adapter,autocompletetextview,Java,Android,Adapter,Autocompletetextview,我有一个自动完成文本视图。我正在从API获取结果,并将结果发送到textchanged上的适配器 这是适配器。 public class ProductSearchAdapter extends BaseAdapter implements Filterable { private Context context; private ArrayList<ProductListModel> originalList; private ArrayList<P
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();
}
}
}
}
关于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) {
}
});
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);
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;
}
}
final GsonBuilder GsonBuilder=new GsonBuilder();
final Gson Gson=gsonBuilder.create();
productList=gson.fromJson(responseString,ProductListModel[].class);
arrayListProducts=新的ArrayList(Arrays.asList(productList));
productsSearchAdapter=新的ProductSearchAdapter(MainActivity.this,arrayListProducts);
autoCompleteTextViewSearch.setThreshold(1);
setAdapter(ProductsAsserAdapter);
使用阵列适配器时使用相同的文本视图,但不使用自定义适配器
产品列表模型:
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) {
}
});
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);
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;
}
}
公共类ProductListModel{
字符串_id;
字符串名;
字符串颜色;
字符串描述;
国际学分;
ProductItemModel类别;
阵列列表子类别;
模型拟合;
产品品牌模式品牌;
ArrayList规则;
模型条件;
阵列图像;
阵列列表大小;
ArrayList imagesSmall;
字符串用户标识;
创建时间长;
批准时间长;
时间长;
长期拒绝;
批准;
布尔拒绝;
布尔特征;
智力状态;
ProductUserProfileModel用户配置文件;
字符串被拒绝\u原因\u id;
字符串类别;
int喜欢;
布尔likedBy;
公共字符串get_id(){
返回_id;
}
公共无效集\u id(字符串\u id){
这个。_id=_id;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getColor(){
返回颜色;
}
公共void setColor(字符串颜色){
这个颜色=颜色;
}
公共字符串getDescription(){
返回说明;
}
公共void集合描述(字符串描述){
this.description=描述;
}
公共int getCredits(){
返回积分;
}
公共无效设置学分(整数学分){
这个。学分=学分;
}
public ProductItemModel getCategory(){
退货类别;
}
公共无效集合类别(ProductItemModel类别){
this.category=类别;
}
公共阵列列表getSubcategories(){
返回子类别;
}
公共无效集合子类别(ArrayList子类别){
this.subcategories=子类别;
}
public ProductItemModel getFit(){
回归拟合;
}
公共无效设置拟合(ProductItemModel拟合){
this.fit=fit;
}
公共产品品牌模型getBrand(){
回归品牌;
}
公共品牌(ProductBrandModel品牌){
这个品牌=品牌;
}
公共ArrayList getRules(){
退货规则;
}
公共无效集合规则(ArrayList规则){
这个。规则=规则;
}
public ProductBrandModel getCondition(){
返回条件;
}
公共无效设置条件(ProductBrandModel条件){
这个条件=条件;
}
公共阵列列表getImages(){
返回图像;
}
公共void集合图像(ArrayList图像){
这个。图像=图像;
}
公共ArrayList getSize(){
返回大小;
}
公共无效集合大小(ArrayList大小){
这个。大小=大小;
}
公共阵列列表getImagesSmall(){
返回图像小;
}
公共无效设置imagesSmall(ArrayList imagesSmall){
this.imagesSmall=imagesSmall;
}
公共字符串getUserId(){
返回用户标识;
}
public void setUserId(字符串userId){
this.userId=userId;
}
public long getTime_created(){
创建的返回时间;
}
已创建公共无效设置时间(已创建长时间){
this.time\u created=创建的时间;
}
公共长时间使用许可证(){
批准的返回时间;
}
公共无效设置时间批准(长时间批准){
this.time\u approved=time\u approved;
}
公共long getTime_精选(){
返回时间;
}
公共无效设置时间(长时间){
this.time\u characterized=time\u characterized;
}
public long getTime_拒绝(){
返回时间被拒绝;
}
公共无效设置时间被拒绝(长时间被拒绝){
this.time\u rejected=time\u rejected;
}
公共布尔值已批准(){
已批准退货;
}
已批准的公共文件集(已批准的布尔值){
这个.已批准的=已批准的;
}
公共布尔值被弹出(){
退货被拒绝;
}
公共无效集已拒绝(布尔值已拒绝){
这个.被拒绝的=被拒绝的;
}
公共布尔值(){
返回功能;
}
公共void集合特征化(布尔特征化){
特色的;特色的;
}
public int getStatus(){
返回状态;
}
公共无效设置状态(int状态){
这个状态=状态;
}
public ProductUserProfileModel getUser_profile(){
返回用户配置文件;
}
public void setUser_配置文件(ProductUserProfileModel用户_配置文件){
this.user\u profile=用户\u profile;
}
公共字符串getRejected_reason_id(){
返回被拒绝的\u原因\u id;
}
public void setRejected_reason_id(字符串rejected_reason_id){
this.rejected_reason_id=rejected_reason_id;
}
公共字符串getCategoryId(){
返回类别ID;
}
public void setCategoryId(字符串categoryId){
this.categoryId=categoryId;
}
公共int getLikes(){
回报喜欢;
}
公共void setLikes(int likes){
this.likes=喜欢;
}
公共布尔isLikedBy(){
喜欢的回报;
}
公共void setLikedBy(布尔值likedBy){
this.likedBy=likedBy;
}
}
您需要