如何实现searchview来过滤资源字符串值(整数)中的arraylist?

时间:2018-08-19 06:50:01

标签: java listview arraylist searchview resourcestring

我如何用源自值字符串的arraylist创建一个listview过滤器,这可能吗? 。我按照本教程进行操作,仅发现在数组列表模型中直接从字符串而不是整数(资源字符串)编写的源代码

问题在于,在过滤器中实现toLowerCase与整数不匹配。

请帮助我,对不起我的英语不好

这是我的代码

public class SearchBox extends Fragment {
public static List<DaftarList> daftarLists;
ListView listView;
ImageView imageView;
SearchView searchView;
TextView textView;


@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final View rootview = inflater.inflate(R.layout.navigasi_searchbox, container, false);
    findViewsById(rootview);
    setProducts();
    ListViewAdapter listViewAdapter = new ListViewAdapter(getActivity(), R.layout.daftarlistview, daftarLists);
    listView.setAdapter(listViewAdapter);
    imageView = (ImageView) rootview.findViewById(R.id.gambar);
    textView = rootview.findViewById(R.id.tv_search);
    searchView = rootview.findViewById(R.id.searchView);

    listView.setVisibility(View.GONE);

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            ListViewAdapter listViewAdapter = new ListViewAdapter(getActivity(), R.layout.daftarlistview, daftarLists);
            listView.setAdapter(listViewAdapter);


            if (TextUtils.isEmpty(newText)) {
                listViewAdapter.getFilter().filter("");
                //     listView.clearTextFilter();
                listView.setVisibility(View.GONE);
                listView.setVisibility(View.INVISIBLE);
                textView.setVisibility(View.VISIBLE);

                return true;

            } else {
                listViewAdapter.getFilter().filter(newText);
                listView.setVisibility(View.VISIBLE);
                textView.setVisibility(View.INVISIBLE);

            }
            return true;
        }

    });


    return rootview;
}

private void setProducts() {
    DaftarList product1 = new DaftarList(1, R.string.Apel, "사과","sagwa", R.mipmap.ic_launcher);
    DaftarList product2 = new DaftarList(2, R.string.Anggur, "포도", "phodo", R.mipmap.ic_launcher);
    DaftarList product3 = new DaftarList(3, R.string.Semangka, "수박", "subak", R.mipmap.ic_launcher);


    daftarLists = new ArrayList<DaftarList>();
    daftarLists.add(product1);
    daftarLists.add(product2);
    daftarLists.add(product3);



}


private void findViewsById(View view) {
    listView = view.findViewById(R.id.listView);
 }

}

[ListViewAdapter]

public class ListViewAdapter extends ArrayAdapter<DaftarList> implements Filterable {                                          

List<DaftarList> daftarLists;
Context context;
int resource;
Filter mFliter;

public ListViewAdapter(Context context, int resource, List<DaftarList> daftarLists) {
    super(context, resource, daftarLists);
    this.context = context;
    this.resource = resource;
    this.daftarLists = daftarLists;

}

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

@Override
public DaftarList getItem(int position) {
    return daftarLists.get(position);
}

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


private class ViewHolder {
    protected TextView judul, romanisasi, arti;
    ImageView imageView;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    final ViewHolder holder;


    if (convertView == null) {
        holder = new ListViewAdapter.ViewHolder();
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(resource, null, true);

        holder.judul = (TextView) convertView.findViewById(R.id.judul);
        holder.romanisasi = (TextView) convertView.findViewById(R.id.deskripsi);
        holder.arti = (TextView) convertView.findViewById(R.id.deskripsi2);
        holder.imageView = (ImageView) convertView.findViewById(R.id.gambar);


        convertView.setTag(holder);
    } else {
        // the getTag returns the viewHolder object set as a tag to the view
        holder = (ViewHolder) convertView.getTag();
    }

    holder.judul.setText(daftarLists.get(position).getName());
    holder.romanisasi.setText(daftarLists.get(position).getDeskripsi());
    holder.arti.setText(daftarLists.get(position).getDeskripsi2());
    holder.imageView.setImageResource(daftarLists.get(position).getImageView());


    return convertView;

}

//filterSearchView
public Filter getFilter() {
    if (mFliter == null)
        mFliter = new CustomFilter();
    return mFliter;

}

 private class CustomFilter extends Filter {

     @Override
     protected FilterResults performFiltering(CharSequence constraint) {
         constraint = constraint.toString().toLowerCase();
         FilterResults result = new FilterResults();
         if (constraint != null && constraint.toString().length() > 0) {
             ArrayList<DaftarList> filt = new ArrayList<DaftarList>(); 

             for (int i = 0; i < daftarLists.size(); i++) {
                 DaftarList m = daftarLists.get(i);
                 if (m.getName().toLowerCase().contains(constraint)) {
                     filt.add(m); //add only items which matches
                 }
             }
             result.count = filt.size();
             result.values = filt;
         } else { // return original list
             synchronized (this) {
                 result.values = daftarLists;
                 result.count = daftarLists.size();
             }
         }
         return result;
     }

     @Override
     protected void publishResults(CharSequence constraint,
                                   FilterResults results) {
         if (results != null) {
             setList((ArrayList<DaftarList>) results.values); 
         } else {
             setList((ArrayList<DaftarList>) daftarLists);
         }
     }
 }

 public void setList(ArrayList<DaftarList> data) {
     daftarLists = data; 
     this.notifyDataSetChanged(); 
 }

0 个答案:

没有答案