适配器未正确过滤 ArrayList

Adapter doesn't filter ArrayList properly

我正在制作一个 fragment,我想在其中搜索名称 (ParseUsers) 并将它们显示在列表中。

适配器在过滤屏幕 (listView) 时完成了它的工作,但是在调用 setOnItemClickListener 时它向我显示了来自非过滤 ArrayList 的错误项目。

用户适配器

public class UserAdapter extends BaseAdapter implements Filterable {

Context context;
ArrayList<User> users;
CustomFilter filter;
ArrayList<User> filteredUsers;

public UserAdapter(Context context, ArrayList<User> users) {
    this.context = context;
    this.users = users;
    this.filteredUsers = users;
}

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

@Override
public Object getItem(int position) {
    return users.get(position);
}

@Override
public long getItemId(int position) {
    return users.indexOf(getItem(position));
}

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

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {
        convertView = inflater.inflate(android.R.layout.simple_list_item_1, null);
    }

    TextView username = (TextView) convertView.findViewById(android.R.id.text1);

    username.setText(users.get(position).getUsername());

    return convertView;
}

@Override
public Filter getFilter() {

    if(filter == null) {
        filter = new CustomFilter();
    }

    return filter;
}

// Inner Class
class CustomFilter extends Filter {

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {

        FilterResults results = new FilterResults();

        if (constraint != null && constraint.length() > 0) {

            constraint = constraint.toString().toUpperCase();

            ArrayList<User> filters = new ArrayList<User>();

            for (int i = 0; i < filteredUsers.size(); i++) {

                if (filteredUsers.get(i).getUsername().toUpperCase().contains(constraint)) {

                    User user = new User(filteredUsers.get(i).getObjectId(), filteredUsers.get(i).getUsername());

                    filters.add(user);
                }
            }

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

        } else {

            results.count = filteredUsers.size();
            results.values = filteredUsers;
        }

        return results;
    }

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

        users = (ArrayList<User>) results.values;
        notifyDataSetChanged();
    }
}
}

片段

ListView listView;
SearchView searchView;

private ArrayList<User> usernameList = new ArrayList<User>();

View view;

UserAdapter adapter;

public WieIsWieFragment() {
    // Required empty public constructor
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
}

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

    getActivity().setTitle("Wie is Wie?");

    view = inflater.inflate(R.layout.fragment_wie_is_wie, container, false);

    adapter = new UserAdapter(getContext(), usernameList);

    searchView = (SearchView) view.findViewById(R.id.searchView);
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String text) {

            return false;
        }

        @Override
        public boolean onQueryTextChange(String text) {

            adapter.getFilter().filter(text);

            return false;
        }
    });

    getData();

    listView = (ListView) view.findViewById(R.id.users_listview);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

            User user = usernameList.get(position);
            Toast.makeText(getContext(), "Username is: " + user.getUsername(), Toast.LENGTH_SHORT).show();
        }
    });

    return view;
}

private void getData() {

    if (usernameList.size() == 0) {

        ParseQuery<ParseUser> query = ParseUser.getQuery();
        query.setLimit(300);
        query.addAscendingOrder("username");
        query.findInBackground(new FindCallback<ParseUser>() {
            @Override
            public void done(List<ParseUser> objects, ParseException e) {
                if (e == null) {
                    if (objects.size() > 0) {
                        for (ParseUser user : objects) {

                            User gebruiker = new User(user.getObjectId(), user.getUsername());
                            gebruiker.setObjectId(user.getObjectId());
                            gebruiker.setUsername(user.getUsername());

                            usernameList.add(gebruiker);
                        }

                        listView.setAdapter(adapter);
                    }
                }
            }
        });

    } else {

        listView.setAdapter(adapter);
    }
}

您正在使用过滤列表中的索引从未过滤列表中检索 User

User user = usernameList.get(position);

你可能想要:

User user = (User) adapter.getItem(position);