使用SearchView Android进行搜索时,图片与listview中的项目不匹配

时间:2018-03-05 14:50:23

标签: android listview fragment searchview

我在使用bundle填充的片段中有listview,我有自定义baseadapter和带有图像和文本的自定义对象,listview可以正常使用图像和文本,但是当我用searchview搜索时,我可以获得文本及其位置和onclicklistener没有任何问题,没问题,问题是我不能拥有带有correcte项目(texttitle)的图像(徽标),图像的范围是它自己的位置而不是项目文本位置

首先是我的自定义对象

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());

        }
    });

请尽可能提供帮助,并提前感谢

3 个答案:

答案 0 :(得分:0)

这是我的适配器类

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;
    }
}

答案 1 :(得分:0)

  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);
}
}

答案 2 :(得分:0)

 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);
}
}