2

我试图弄清楚如何将多个过滤器(文本框和另一个字段)应用于列表视图。

现在,我正在使用文本框进行过滤,但我想添加一个复选框并过滤行,如果它们有一个值为真或假的字段。所以,我可以同时应用两个过滤器。

现在,我得到的是这样的:

public class ListUsersDialog extends AbstractDialog {

    //TODO
    private Button okButton;
    private Button cancelButton;

    private ListView lv;     
    //private ArrayAdapter<String> adapter;
    private ArrayAdapter<UserResponse> adapter;
    private EditText inputSearch;   
    private EditText inputSearch2;   
    private String room;
    //private String users[] = {};
    private UserResponse users[] = {};



    public ListUsersDialog() {
        super();
        // TODO Auto-generated constructor stub
    }

    public ListUsersDialog(String room) {
        this.room = room;
    }

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

        View view = inflater.inflate(R.layout.dialog_list_users, container);

        //Se consulta por la lista de los usuarios, los parametros que se pasan es por si se quiere customizar el resultado en el futuro.
        AsyncGUI asyncDialogs = new AsyncGUI(this);
        asyncDialogs.execute(new RequestMessage(ConstantsRooms.GET_USERS, room , ((AbstractActivity)getActivity()).getUserName()));     
        getDialog().setTitle("Invitar a usuarios");

         // Listview Data     
        lv = (ListView) view.findViewById(R.id.list_view_users);
        inputSearch = (EditText) view.findViewById(R.id.inputSearch);

        // Adding items to listview        
        //adapter = new ArrayAdapter<String>(this.getActivity(), R.layout.list_item_user, R.id.userNameSendInvitation, users);
        adapter = new ArrayAdapter<UserResponse>(this.getActivity(), R.layout.list_item_user, R.id.userNameSendInvitation, users);
        lv.setAdapter(adapter);        

        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

                // selected item
                String userToSendMessage = (String)parent.getAdapter().getItem(position);               
                //Se hace una llamada a GCM con el usuario que crea la partida y a quien va dirigida la invitacion

                AsyncGUI async = new AsyncGUI(ListUsersDialog.this);
                async.execute(new RequestMessage(ConstantsRooms.SEND_GCM_MESSAGE_INVITATION, 
                                                ((AbstractActivity)getActivity()).getUserName(), 
                                                room,
                                                userToSendMessage));

            }
          });

        //Filter.
        inputSearch.addTextChangedListener(new TextWatcher() { 
            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {                
                ListUsersDialog.this.adapter.getFilter().filter(cs);
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) { 
            }

            @Override
            public void afterTextChanged(Editable arg0) {
            }
    });

}

我认为,这是典型的代码,我不知道是否有人可以给我一些线索来做我正在尝试的事情。

这是我的适配器:

public class CustomUsersAdapter extends ArrayAdapter<UserResponse> {

    private List<UserResponse> usersList;
    private Context context;

    public CustomUsersAdapter(List<UserResponse> usersList, Context ctx) {
        super(ctx, R.layout.list_item_user, usersList);
        this.usersList = usersList;
        this.context = ctx;
    }

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

        // First let's verify the convertView is not null
        if (convertView == null) {
            // This a new view we inflate the new layout
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.list_item_user, parent,
                    false);
        }
        // Now we can fill the layout with the right values
        TextView name = (TextView) convertView
                .findViewById(R.id.txt_userNameSendInvitation);
        TextView isFriend = (TextView) convertView
                .findViewById(R.id.txt_friend);
        UserResponse user = usersList.get(position);

        name.setText(user.getUserName());
        isFriend.setText("Friend:" + user.isbFriend());

        return convertView;
    }

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

    public UserResponse getItem(int position) {
        return usersList.get(position);
    }

    public long getItemId(int position) {
        return usersList.get(position).hashCode();
    }

}

甚至,如果我在我的适配器中创建一个过滤器,我无法弄清楚如何过滤两个不同的字段,因为我需要两个过滤器类,我认为这是不可能的......或者是吗?

SOLUTION: 如何在一个适配器上应用多个过滤器?

4

1 回答 1

0

您应该可以通过setFilterQueryProvider在适配器上添加功能来做到这一点:http: //developer.android.com/reference/android/widget/CursorAdapter.html#setFilterQueryProvider(android.widget.FilterQueryProvider)

每次addTextChangedListener调用您时都会触发此功能。因此,您可以在多个字段上设置多个侦听器。

您需要为您的适配器添加此代码并实现它:

mAdapter.setFilterQueryProvider(new FilterQueryProvider() {
        public Cursor runQuery(CharSequence constraint) {           
            //return here your updated cursor
        }
    });

PS:我在具有 5 个不同过滤器的 ListView 上使用它

于 2013-05-13T09:09:47.427 回答