Android搜索过滤列表视图

时间:2017-05-09 10:58:22

标签: android listview firebase

好的,我已经尝试了几个在这里找到的答案,我仍然无法做到这一点,我想主要是因为我不知道所有这些过滤器实际上是如何工作的。所以我想知道你们是否可以帮助我使用我的代码。我现在正在关注这里的答案:

Custom Listview Adapter with filter Android

我不明白" mSeachableAdapter"在他的MainActivity来自。有人可以帮我修复我的代码吗?另外,请确认我是否使用正确的方法过滤列表视图。

MainActivity:

et.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) {
           //this one
 mSearchableAdapter.getFilter().filter(charSequence.toString());
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });

CustomAdapter.java :(我从链接开始的过滤器适配器)

public class CustomAdapter extends BaseAdapter implements Filterable {

private List<String>originalData = null;
private List<String>filteredData = null;
private LayoutInflater mInflater;
private ItemFilter mFilter = new ItemFilter();

public CustomAdapter(Context context, List<String> data) {
    this.filteredData = data ;
    this.originalData = data ;
    mInflater = LayoutInflater.from(context);
}

public int getCount() {
    return filteredData.size();
}

public Object getItem(int position) {
    return filteredData.get(position);
}

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

public View getView(int position, View convertView, ViewGroup parent) {
    // A ViewHolder keeps references to children views to avoid unnecessary calls
    // to findViewById() on each row.
    ViewHolder holder;

    // When convertView is not null, we can reuse it directly, there is no need
    // to reinflate it. We only inflate a new View when the convertView supplied
    // by ListView is null.
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.layout_team_list, null);

        // Creates a ViewHolder and store references to the two children views
        // we want to bind data to.
        holder = new ViewHolder();
        holder.text = (TextView) convertView.findViewById(R.id.textViewName);

        // Bind the data efficiently with the holder.

        convertView.setTag(holder);
    } else {
        // Get the ViewHolder back to get fast access to the TextView
        // and the ImageView.
        holder = (ViewHolder) convertView.getTag();
    }

    // If weren't re-ordering this you could rely on what you set last time
    holder.text.setText(filteredData.get(position));

    return convertView;
}

static class ViewHolder {
    TextView text;
}

public Filter getFilter() {
    return mFilter;
}

private class ItemFilter extends Filter {
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {

        String filterString = constraint.toString().toLowerCase();

        FilterResults results = new FilterResults();

        final List<String> list = originalData;

        int count = list.size();
        final ArrayList<String> nlist = new ArrayList<String>(count);

        String filterableString ;

        for (int i = 0; i < count; i++) {
            filterableString = list.get(i);
            if (filterableString.toLowerCase().contains(filterString)) {
                nlist.add(filterableString);
            }
        }

        results.values = nlist;
        results.count = nlist.size();

        return results;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        filteredData = (ArrayList<String>) results.values;
        notifyDataSetChanged();
    }

}
}

TeamList.java :(我只是把它放在这里以防万一需要)

public class TeamList extends ArrayAdapter<Team> {
private Activity context;
List<Team> teams;



public TeamList(Activity context, List<Team> teams) {
    super(context, R.layout.layout_team_list, teams);
    this.context = context;
    this.teams = teams;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    View listViewItem = inflater.inflate(R.layout.layout_team_list, null, true);

    TextView textViewName = (TextView) listViewItem.findViewById(R.id.textViewName);
    TextView textViewSport = (TextView) listViewItem.findViewById(R.id.textViewSport);
    TextView textViewDate = (TextView) listViewItem.findViewById(R.id.textViewDate);
    TextView textViewTime = (TextView) listViewItem.findViewById(R.id.textViewTime);
    final TextView textViewNumber = (TextView) listViewItem.findViewById(R.id.textViewNumber);
    ImageView icon = (ImageView) listViewItem.findViewById(R.id.icon);


    final Team team = teams.get(position);
    textViewName.setText(team.getTeamName());
    textViewSport.setText(team.getTeamSport());
    textViewDate.setText(team.getDate());
    textViewTime.setText(team.getTime());

    databaseMembers.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            long childCount = dataSnapshot.child("members").child(team.getTeamName()).child("teamMember").getChildrenCount();
            textViewNumber.setText(childCount + "/" + String.valueOf(team.getTeamNumber()));
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });


    String mDrawableName = team.getTeamSport();
    try{
        int ImgID = context.getResources().getIdentifier(mDrawableName, "drawable", context.getPackageName());
        Drawable drawable = context.getResources().getDrawable(ImgID);
        icon.setBackground(drawable);
    }
    catch (Exception e){
        e.printStackTrace();
    }

    return listViewItem;
}


}

真的希望有人可以指导我完成所有这些,如果有任何不充分的信息,请告诉我。真的很感激。

0 个答案:

没有答案