过滤多行自定义ListView

时间:2018-02-15 07:31:12

标签: android listview

我正在android中创建一个论坛样式活动,其中论坛问题及其详细信息从firebase数据库收集,然后显示在自定义多行列表视图中。现在我想使用适配器过滤器实现搜索选项。我已经从web和stackoverflow中学到了很多教程,但这些都没有实际工作。这是我在stackoverflow中关注的link之一。问题是在搜索之后,过滤器总是显示第一个listview项而不进行过滤。

这是我的活动,我使用String数组填充Listview:

final String key[] = new String[keysList.size()];
    keysList.toArray(key);
    final String category[] = new String[categoryList.size()];
    categoryList.toArray(category);
    final String name[] = new String[namesList.size()];
    namesList.toArray(name);
    final String location[] = new String[locationList.size()];
    locationList.toArray(location);
    final String description[] = new String[questionsList.size()];
    questionsList.toArray(description);
    final String title[] = new String[titlesList.size()];
    titlesList.toArray(title);
    final String date[] = new String[dateList.size()];
    dateList.toArray(date);      
    final int randomclrs[] = new int[dateList.size()]; // random clr string array with the same size as data
    for (int i =0; i< randomclrs.length;i++){

        Random random = new Random();
        randomclrs[i] = Color.rgb(random.nextInt(255), random.nextInt(255), random.nextInt(255));
    }     
    adapter = new custom_forum_list_adapter(forum.this,name,category,date,location,key,title,description,randomclrs);
   list.setAdapter(adapter);

custom_forum_list_adapter:

注意:我正在过滤类别字符串数组。

public class custom_forum_list_adapter extends ArrayAdapter<String> implements Filterable {

private final Activity context;
private final String[] name;
private final String[] category;
private final String[] date;
private final String[] location;
private final String[] key;
private final String[] title;
private final String[] question;
private final int[] color;
List<String> arrayListtofilter;
List<String> mOriginalValues;

public custom_forum_list_adapter(Activity context,
                                 String[] name, String[] category, String[] date, String[] location, String[] key, String[] title,
                                 String[] question, int[] color) {
    super(context, R.layout.activity_fixers_in_list, name);
    this.context = context;
    this.name = name;
    this.category = category;
    this.date = date;
    this.location = location;
    this.key = key;
    this.title = title;
    this.question = question;
    this.color = color;
    this.arrayListtofilter = new ArrayList<String>(Arrays.asList(category));
}

@Override
public int getCount() {
    return arrayListtofilter.size();
}
@Override
public View getView(int position, View view, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    View rowView= inflater.inflate(R.layout.forum_listview, parent, false);
    TextView txtname = (TextView) rowView.findViewById(R.id.forum_name);
    txtname.setText(name[position]);
    TextView txtcategory = (TextView) rowView.findViewById(R.id.forum_category);
    txtcategory.setText(category[position]);
    TextView txtdate = (TextView) rowView.findViewById(R.id.forumdate);
    txtdate.setText(date[position]);
    TextView txtlocation = (TextView) rowView.findViewById(R.id.forumlocation);
    txtlocation.setText(location[position]);
    TextView txttitle = (TextView) rowView.findViewById(R.id.forum_title);
    txttitle.setText(title[position]);
    TextView txtdescription = (TextView) rowView.findViewById(R.id.forumdescription);
    txtdescription.setText(question[position]);
    TextView firstletter = (TextView) rowView.findViewById(R.id.forum_firstletter);
    firstletter.setText(name[position].substring(0,1));
    ImageView img = (ImageView) rowView.findViewById(R.id.randomcolor);
    img.setColorFilter(color[position]);
    return rowView;
}
@Override
public Filter getFilter() {
    Filter filter = new Filter() {

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint,FilterResults results) {

            arrayListtofilter = (List<String>) results.values; // has the filtered values
            notifyDataSetChanged();  // notifies the data with new filtered values
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();        // Holds the results of a filtering operation in values
            List<String> FilteredArrList = new ArrayList<String>();

            if (mOriginalValues == null) {
                mOriginalValues = new ArrayList<String>(arrayListtofilter); 
            }              
            if (constraint == null || constraint.length() == 0) {

                // set the Original result to return
                results.count = mOriginalValues.size();
                results.values = mOriginalValues;
            } else {
                constraint = constraint.toString().toLowerCase();
                for (int i = 0; i < mOriginalValues.size(); i++) {
                    String data = mOriginalValues.get(i);
                    if (data.toLowerCase().startsWith(constraint.toString())) {
                        FilteredArrList.add(data);
                    }
                }
                // set the Filtered result to return
                results.count = FilteredArrList.size();
                results.values = FilteredArrList;
            }
            return results;
        }
    };
    return filter;
}

0 个答案:

没有答案