在筛选列表视图中获取项目的错误位置

时间:2018-05-29 12:46:42

标签: android listview filter

我有一个列表,用于在用户输入搜索字符串时过滤数据。当用户单击搜索项时,它会打开新活动。问题是它打开了错误的意图。当用户单击该项目时,位置会因过滤列表视图而更改。它始终返回第一项的意图,因为匹配的搜索结果始终首先显示。我错过了其中的一小部分,请任何帮助将不胜感激。

这是我的自定义适配器

public class Searchitemadapter extends BaseAdapter implements Filterable {
 private Home activity;
 private FriendFilter friendFilter;
 private ArrayList<B_allProducts> friendList;
 private ArrayList<B_allProducts> filteredList;

public Searchitemadapter(Home activity, ArrayList<B_allProducts> friendList) {
    this.activity = activity;
    this.friendList = friendList;
    this.filteredList = friendList;

    getFilter();
}

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

@Override
public Object getItem(int i) {
    return filteredList.get(i);
}

@Override
public long getItemId(int i) {
    return i;
}

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

    final ViewHolder holder;
    final B_allProducts user = (B_allProducts) getItem(position);

    if (view == null) {
        LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = layoutInflater.inflate(R.layout.lv_container_searchitem, parent, false);
        holder = new ViewHolder();
        holder.name = (TextView) view.findViewById(R.id.lv_search_pname);

        view.setTag(holder);
    } else {
        // get view holder back
        holder = (ViewHolder) view.getTag();
    }

    holder.name.setText(user.getProductName());

    return view;
}

@Override
public Filter getFilter() {
    if (friendFilter == null) {
        friendFilter = new FriendFilter();
    }

    return friendFilter;
}

static class ViewHolder {
    TextView name;
}
private class FriendFilter extends Filter {

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults filterResults = new FilterResults();
        if (constraint!=null && constraint.length()>0) {
            ArrayList<B_allProducts> tempList = new ArrayList<B_allProducts>();

            for (B_allProducts user : friendList) {
                if (user.getProductName().toLowerCase().contains(constraint.toString().toLowerCase())
                        ||user.getProductKeywordG().toLowerCase().contains(constraint.toString().toLowerCase())
                        ||user.getProductKeyword().toLowerCase().contains(constraint.toString().toLowerCase()))
                {
                    tempList.add(user);
                }
            }

            filterResults.count = tempList.size();
            filterResults.values = tempList;
        } else {
            filterResults.count = friendList.size();
            filterResults.values = friendList;
        }

        return filterResults;
    }

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

这是我的列表视图onItemClickListener

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


            String pos = aphashmap.get(position).get("ProductID");
            Intent i = new Intent(SearchableActivity.this, DisplaySingleProduct.class);
            i.putExtra("ProductID",pos);

            startActivity(i);
        }
    });

1 个答案:

答案 0 :(得分:1)

您应该使用父对象和getAdapter()方法来获得正确的位置。

例如;

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

        int positionOfItem=parent.getAdapter().getItem(position)
        String pos = aphashmap.get(positionOfItem).get("ProductID");
        Intent i = new Intent(SearchableActivity.this, DisplaySingleProduct.class);
        i.putExtra("ProductID",pos);
        startActivity(i);
    }
});