列表视图搜索不适用于项目单击。返回原始项目位置

时间:2017-09-29 08:47:27

标签: java android listview

我在searchview中遇到了问题。 例如,如果我的原始列表视图是: - Aa Ab Ac Ad。 但是当我在搜索栏中搜索例如b的查询时,它会给出: - Ab。 但当我点击项目时,它会给出位置值: - Aa。

这是我的列表视图活动: -

HashMap<String, String> userMap = Login.userMap;
final ArrayList<String> userList = new ArrayList(userMap.keySet());
            listView = (ListView) findViewById(R.id.listView);
            adapter = new ListAdapter(this, userMap);
            listView.setAdapter(adapter);
            listView.setTextFilterEnabled(true);
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                    user = userList.get(position);
                    new GetDetails().execute(new Void[0]);
                }
            });

searchView.setMenuItem(item);
        searchView.setOnQueryTextListener(new MaterialSearchView.OnQueryTextListener() {
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            public boolean onQueryTextChange(String newText) {
                Log.i("newText", newText);
                if (newText.isEmpty()) {
                    adapter.filter("");
                    listView.clearTextFilter();
                } else {
                    adapter.filter(newText);
                }
                return true;
            }
        });

这是我的适配器: -

class ListAdapter extends BaseAdapter {
private static LayoutInflater inflater = null;
private Activity activity;
private HashMap<String, String> data;
private List<String> username = new ArrayList();
private ArrayList<HashMap<String, Object>> list;

public ListAdapter(Activity a, HashMap<String, String> d) {
    this.activity = a;
    this.data = d;
    inflater = (LayoutInflater) this.activity.getSystemService("layout_inflater");
}

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

public Object getItem(int position) {
    return position;
}

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

public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    if (convertView == null) {
        vi = inflater.inflate(R.layout.main_list_adapter, null);
    }
    ImageView image = (CircularImageView) vi.findViewById(R.id.img);
    ((TextView) vi.findViewById(R.id.txt)).setText(username.get(position));
    Picasso.with(this.activity.getApplicationContext()).load(data.get(this.username.get(position))).placeholder(R.mipmap.empty_photo).resize(70, 70).into(image);
    return vi;
}

public void filter(String charText) {
    username.clear();
    if (charText.length() == 0) {
        username.addAll(data.keySet());
    } else {
        for (String s : data.keySet()) {
            if (s.contains(charText)) {
                username.add(s);
            }
        }
    }
    notifyDataSetChanged();
}


public void refresh(HashMap<String, String> newData) {
    data = newData;
    username.clear();
    username.addAll(data.keySet());
    notifyDataSetChanged();
}}

帮助将非常感谢伙计们。我环顾四周,但我无法找到它。

2 个答案:

答案 0 :(得分:1)

将适配器中的getitem()覆盖为

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

并点击使用adapter.getItem(position);

这将有效;

答案 1 :(得分:0)

点击返回的位置对应于数据列表中项目的位置。因此,在过滤期间,您正在更改列表username,因此返回的位置将是列表中的元素。