使用搜索视图Android进行搜索时,图像与列表视图中的项目不匹配



我在带有捆绑包的活动填充的片段中具有列表视图,我有自定义的基本适配器和带有图像和文本的自定义对象,listview 对图像和文本工作正常,但是当我使用 searchview 进行搜索时,我可以获取文本并且它的位置和 onclicklistener 可以没有任何问题,问题是我不能将图像(徽标(与正确的项目(文本标题(一起使用, 图像按其自身位置而不是项目文本位置排列

首先是我的自定义对象

public class Channel {
public String name;
public String logoUrl;
public Channel(String name, String logoUrl) {
    this.name = name;
    this.logoUrl = logoUrl;
}
@Override
public String toString() {
    return "Channel {" +
            "name='" + name + ''' +
            ", logoUrl='" + logoUrl + ''' +
            '}';
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getLogoUrl() {
    return logoUrl;
}
public void setLogoUrl(String logoUrl) {
    this.logoUrl = logoUrl;
}

}

这是我的自定义底座适配器

public class ChannelAdapter extends BaseAdapter implements Filterable{
private Activity context;
private ArrayList<Channel> channelNames = new ArrayList<>(); //mList
private ArrayList<Channel> channelLogos = new ArrayList<>();
private ArrayList<Channel> tmpNames= null;
private ArrayList<Channel> tmpLogos= null;
private CustomFilter myFilter = new CustomFilter();
ColorSpace.Model model;


public ChannelAdapter(Context context, ArrayList<Channel> channels, ArrayList<Channel> logos){

    this.context = (Activity) context;
    this.channelNames = channels;
    this.channelLogos = logos;
    this.tmpNames = channels;
    this.tmpLogos = logos;

    //    Names.addAll(channelLogos);

}

@Override
public int getCount() {
    // return Names.size();
    return tmpNames.size();
}
@Override
public String getItem(int position) {
    return String.valueOf(tmpNames.get(position));
}
@Override
public long getItemId(int position) {
    return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row =null;
    if(convertView==null) {
        LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row=inflater.inflate(R.layout.custom_row,parent, Boolean.parseBoolean(null));

    }else{
        row=convertView;
    }
    TextView textView= (TextView) row.findViewById(R.id.textv);
    ImageView image=(ImageView) row.findViewById(R.id.imageView);
    textView.setText((CharSequence) tmpNames.get(position));

    Picasso.with(context)
            .load(String.valueOf(tmpLogos.get(position)))
            .into(image);
    return row;
}

@Override
public Filter getFilter() {
    if (myFilter == null) {
        myFilter = new CustomFilter();
    }
    return myFilter;
}
public class CustomFilter extends Filter{
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        // TODO Auto-generated method stub
        String filterString = constraint.toString().toLowerCase();
        FilterResults results = new FilterResults();
        final ArrayList<Channel> list = channelNames; //remettre channelNames
        int count = list.size();
        final ArrayList<String> nlist = new ArrayList<String>(count);
        String filterableString ;
        for (int i = 0; i < count; i++) {
            filterableString = String.valueOf(list.get(i));
            if (filterableString.toLowerCase().contains(filterString)) {
                nlist.add(filterableString);
            }
        }
        results.values = nlist;
        results.count = nlist.size();
        return results;
    }


    @Override
    protected void publishResults(CharSequence constraint,FilterResults results) {
        // TODO Auto-generated method stub
        // Names.clear();
        // addAll((List<Channel>) results.values);
        tmpNames = (ArrayList<Channel>) results.values;
        notifyDataSetChanged();
    }
}

}

并完成我的OnqueryTextChange

 @Override
        public boolean onQueryTextChange(String newText) {
            ((ChannelAdapter)listView.getAdapter()).getFilter().filter(newText.toString());
        }
    });

如果可能,请寻求帮助并提前感谢

这是我的适配器类

public class ChannelAdapter extends MatchableArrayAdapter<Channel>{
    private Activity context;
    private ArrayList<Channel> channelNames = new ArrayList<>(); //mList
    private ArrayList<Channel> channelLogos = new ArrayList<>();
    private ArrayList<Channel> tmpNames= new ArrayList<Channel>();
    private ArrayList<Channel> tmpLogos= null;
     boolean notifyChanged = false;
     public ChannelAdapter(Context context, ArrayList<Channel> channels, 
     ArrayList<Channel> logos){
        super(context, 0, channels);

        this.context = (Activity) context;
        this.channelNames = channels;
        this.channelLogos = logos;
        this.tmpNames = channels;
        this.tmpLogos = channels;

        //    Names.addAll(channelLogos);


    }

    @Override
    public int getCount() {
        // return Names.size();
        return channelNames.size();
    }
    @Override
    public Channel getItem(int position) {
        return channelNames.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row =null;
        if(convertView==null) {
            LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row=inflater.inflate(R.layout.custom_row,parent, Boolean.parseBoolean(null));

        }else{
            row=convertView;
        }
       //  TextView textView = null;
        TextView textView= (TextView) row.findViewById(R.id.textv);
      //  ((TextView)row.findViewById(R.id.textv)).getText().toString();
        ImageView image=(ImageView) row.findViewById(R.id.imageView);

        textView.setText((CharSequence) channelNames.get(position));


        Picasso.with(context)
                .load(String.valueOf(channelLogos.get(position)))
                .into(image);
        return row;
    }

    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                final FilterResults oReturn = new FilterResults();
                final ArrayList<Channel> results = new ArrayList<Channel>();
                if (channelNames == null)
                    channelNames = tmpNames;
                if (constraint != null) {
                    if (channelNames != null && channelNames.size() > 0) {
                        for (final Channel g : channelNames) {
                            if (g.getName().toLowerCase()
                                    .contains(constraint.toString()))
                                results.add(g);
                        }
                    }
                    oReturn.values = results;
                }
                return oReturn;
            }
            @SuppressWarnings("unchecked")
            @Override
            protected void publishResults(CharSequence constraint,
                                          FilterResults results) {
                tmpNames = (ArrayList<Channel>) results.values;
                notifyDataSetChanged();
            }
        };
    }
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
        notifyChanged = true;
    }
}
 public class Channel {
private String name;
private String logoUrl;
public Channel(String name, String logoUrl) {
    this.name = name;
    this.logoUrl = logoUrl;
}
@Override
public String toString() {
    return "Channel{" +
            "name='" + name + ''' +
            ", logoUrl='" + logoUrl + ''' +
            '}';
}
public String getName() {
    return String.valueOf(name);
}
public void setName(ArrayList<String> name) {
    this.name = String.valueOf(name);
}
public String getLogoUrl() {
    return String.valueOf(logoUrl);
}
public void setLogoUrl(ArrayList<String> logoUrl) {
    this.logoUrl = String.valueOf(logoUrl);
}
}
  public class ChannelAdapter extends MatchableArrayAdapter<Channel> {
  public ChannelAdapter(Context context, List<Channel> objects) {
    super(context, R.layout.custom_row, objects);
}
@Override
protected void onBind(Channel item, View itemView, int position) {
    TextView tv = (TextView) itemView.findViewById(R.id.textv);
    tv.setText(item.getName());
    ImageView iv = (ImageView) itemView.findViewById(R.id.imageView);
    Picasso.with(iv.getContext())
            .load(item.getLogoUrl())
            .into(iv);
}
@Override
protected boolean matches(Channel value, CharSequence constraint, 
CharSequence lowerCaseConstraint) {
    return value.getName().toLowerCase().contains(lowerCaseConstraint);
}
}

最新更新