我试图弄清楚如何将多个过滤器(文本框和另一个字段)应用于列表视图。
现在,我正在使用文本框进行过滤,但我想添加一个复选框并过滤行,如果它们有一个值为真或假的字段。所以,我可以同时应用两个过滤器。
现在,我得到的是这样的:
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: 如何在一个适配器上应用多个过滤器?