使用带有文本和图像的自定义适配器在列表视图中搜索

时间:2018-01-05 02:37:13

标签: android listview android-fragments android-adapter android-filterable

我正在我的应用中配置资源,以使用自定义适配器在Listview中进行搜索。我使用EditText键入字符串并将文本传递给addTextChangedListener事件上的Adapter,但它不起作用。

我使用“implements Filterable”配置适配器并使用“.setTextFilterEnabled(true)”启用Listview,但不起作用。

我看到我必须实现“public Filter getFilter()”但我不知道该怎么做。

当我在EditText中键入一些单词时,比如“cel”或“12”,过滤器就会运行,但结果总是相同的:Listview中的前两项,无论Listview中的内容是什么(内容列表视图是随机的。

在片段活动的片段下面:

public class VideosFragment extends Fragment {

private ListView listView;
private ArrayList<String> idVideo = new ArrayList<>();
private ArrayList<String> titleVideo = new ArrayList<>();
private ArrayList<String> descVideo = new ArrayList<>();
private ArrayList<String> urlVideo = new ArrayList<>();
private ArrayList<String> channelTitle = new ArrayList<>();
private ArrayList<String> canalTitle = new ArrayList<>();
private ArrayList<String> canalId = new ArrayList<>();
private CustomFiltraCanaisAdapter customFiltraCanaisAdapter;
private EditText editText;

CustomVideoAdapter customVideoAdapter;

public VideosFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_videos, container, false);


    // Configure Listview
    listView = (ListView)view.findViewById(R.id.listView_videos);

    //Create search parameters
    editText = (EditText) view.findViewById(R.id.searchList);
    listView.setTextFilterEnabled(true);
    editText.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) {
            String text = editText.getText().toString().toLowerCase(Locale.getDefault());
            VideosFragment.this.customVideoAdapter.getFilter().filter(text);
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });

以及我的CustomAdapter:

public class CustomVideoAdapter extends ArrayAdapter<String> implements Filterable {

private final Activity context;
private final ArrayList<String> videoTitle;
private final ArrayList<String> videoDesc;
private final ArrayList<String> videoId;
private final ArrayList<String> channelTitle;
private final ArrayList<String> imgurl;

public CustomVideoAdapter(Activity context, ArrayList<String> videoTitle,
                                            ArrayList<String> videoId,
                                            ArrayList<String> channelTitle,
                                            ArrayList<String> imgurl,
                                            ArrayList<String> videoDesc) {
    super(context, R.layout.custom_lista_videos, videoTitle);
    // TODO Auto-generated constructor stub

    this.context = context;
    this.videoTitle = videoTitle;
    this.videoDesc = videoDesc;
    this.videoId = videoId;
    this.channelTitle = channelTitle;
    this.imgurl = imgurl;
}
public View getView(int position,View view,ViewGroup parent) {
    LayoutInflater inflater=context.getLayoutInflater();
    View rowView=inflater.inflate(R.layout.custom_lista_videos, null,true);

    TextView txtTitle = (TextView) rowView.findViewById(R.id.txtVideoTitle);
    ImageView imgPhoto = (ImageView) rowView.findViewById(R.id.imgPhoto);
    TextView txtChannelTitle = (TextView) rowView.findViewById(R.id.txtChannelTitle);
    TextView txtVideoId = (TextView) rowView.findViewById(R.id.txtVideoId);
    TextView txtVideoDesc = (TextView) rowView.findViewById(R.id.txtVideoDesc);

    txtTitle.setText(videoTitle.get(position));
    Picasso.with(context).load(imgurl.get(position)).into(imgPhoto);
    txtChannelTitle.setText(channelTitle.get(position));
    txtVideoId.setText(videoId.get(position));
    txtVideoDesc.setText(videoDesc.get(position));
    return rowView;

};

缺少什么?

经过一些帮助我尝试创建(和使用)模型类,但我仍然没有进展,因为我没有弄清楚如何在“CustomAdapter”上更改我的代码以正确使用此代码。

我的模型类如下:

公共类FilteredVideoAdapter {

private ArrayList<String> storedVideoTitle = null;
private ArrayList<String> storedVideoDesc = null;
private ArrayList<String> storedVideoId = null;
private ArrayList<String> storedChannelTitle = null;
private ArrayList<String> storedImgurl = null;

public FilteredVideoAdapter(){

}

public ArrayList<String> getStoredVideoTitle() {
    return storedVideoTitle;
}

public void setStoredVideoTitle(ArrayList<String> storedVideoTitle) {
    this.storedVideoTitle = storedVideoTitle;
}

public ArrayList<String> getStoredVideoDesc() {
    return storedVideoDesc;
}

public void setStoredVideoDesc(ArrayList<String> storedVideoDesc) {
    this.storedVideoDesc = storedVideoDesc;
}

public ArrayList<String> getStoredVideoId() {
    return storedVideoId;
}

public void setStoredVideoId(ArrayList<String> storedVideoId) {
    this.storedVideoId = storedVideoId;
}

public ArrayList<String> getStoredChannelTitle() {
    return storedChannelTitle;
}

public void setStoredChannelTitle(ArrayList<String> storedChannelTitle) {
    this.storedChannelTitle = storedChannelTitle;
}

public ArrayList<String> getStoredImgurl() {
    return storedImgurl;
}

public void setStoredImgurl(ArrayList<String> storedImgurl) {
    this.storedImgurl = storedImgurl;
}

public FilteredVideoAdapter withFilteredVideoAdapter(
        ArrayList<String> storedVideoTitle,
        ArrayList<String> storedVideoDesc,
        ArrayList<String> storedVideoId,
        ArrayList<String> storedChannelTitle,
        ArrayList<String> storedImgurl){

    this.storedVideoTitle = storedVideoTitle;
    this.storedVideoDesc = storedVideoDesc;
    this.storedVideoId = storedVideoId;
    this.storedChannelTitle = storedChannelTitle;
    this.storedImgurl = storedImgurl;

    return this;
}

}

2 个答案:

答案 0 :(得分:1)

这是你在适配器中必须做的事情,你可能需要做一些调整: -

 @Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults results = new FilterResults();



                // If the constraint (search string/pattern) is null
                // or its length is 0, i.e., its empty then
                // we just set the `values` property to the
                // original contacts list which contains all of them
                if (constraint == null || constraint.length() == 0) {
                    results.values =   videoTitle;;
                    results.count = videoTitle.size();
                }
                else {
                    // Some search copnstraint has been passed
                    // so let's filter accordingly
                    ArrayList<String> filteredTitle = new ArrayList<String>();

                    // We'll go through all the title and see
                    // if they contain the supplied string
                    for (String c : string) {
                        if (string.toUpperCase().contains( constraint.toString().toUpperCase() )) {
                            // if `contains` == true then add it
                            // to our filtered list
                            filteredTitle.add(c);
                        }
                    }

                    // Finally set the filtered values and size/count
                    results.values = filteredTitle;
                    results.count = filteredTitle.size();
                }

                // Return our FilterResults object
                return results;
            }
            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                mList = (ArrayList<String>) results.values;
                notifyDataSetChanged();
            }
        };
    }

希望这会对你有所帮助。

答案 1 :(得分:0)

您可以在不使用可过滤的情况下执行此操作,请尝试使用此代码

您的活动代码

vendorSearchEt.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (vendorSearchEt.getText().toString().length() == 0) {
                if (vendorsListAdapter != null) {
                    vendorsListAdapter.filter("");
                }
            } else {
                String text = vendorSearchEt.getText().toString().toLowerCase(Locale.getDefault());
                if (vendorsListAdapter != null) {
                    vendorsListAdapter.filter(text);
                }
            }

        }
    });

适配器类

public class VendorsListAdapter extends BaseAdapter {

// Declare Variables \\
Context mContext;
LayoutInflater inflater;
private List<VendorsList> vendorsLists = null;
private ArrayList<VendorsList> arrayListVendorsList = null;
AppCompatActivity act;

public VendorsListAdapter(Context context,
                          List<VendorsList> vendorsLists) {
    mContext = context;
    this.vendorsLists = vendorsLists;
    arrayListVendorsList = new ArrayList<>(vendorsLists);
    inflater = LayoutInflater.from(mContext);
    act = (AppCompatActivity) context;

}

public class ViewHolder {

    TextView vendorNameTv;
}

@Override
public int getCount() {
    return vendorsLists.size();
}

@Override
public VendorsList getItem(int position) {
    return vendorsLists.get(position);
}

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

public View getView(final int position, View view, ViewGroup parent) {
    final ViewHolder holder;
    if (view == null) {
        holder = new ViewHolder();
        view = inflater.inflate(R.layout.lv_items_vendors_list, null);

        holder.vendorNameTv = view.findViewById(R.id.lv_items_vendors_name_tv);
        holder.vendorNameTv.setTypeface(StaticMethods.customFont(act, "Quicksand-Regular.otf"));

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

    holder.vendorNameTv.setText(vendorsLists.get(position).getVendorName());

    return view;
}

/**
 * Filter Class for item searching
 *
 * @param charText Text to be searched
 */
public void filter(String charText) {
    charText = charText.toLowerCase(Locale.getDefault());
    vendorsLists.clear();

    if (charText.length() == 0) {
        vendorsLists.addAll(arrayListVendorsList);
    } else {
        for (VendorsList vendorsList : arrayListVendorsList) {
            if (vendorsList.getVendorName().toLowerCase(Locale.getDefault())
                    .contains(charText)) {
                vendorsLists.add(vendorsList);
            }
        }
    }
    notifyDataSetChanged();
}}