如何从自定义适配器实现原始Listview项目位置实现可过滤

时间:2017-09-17 19:37:19

标签: android listview

我是Android的新手,想要一些代码的帮助。我有一个自定义列表视图,实现可过滤。现在我想要做的是即使在过滤结果后获得原始列表视图项目位置。例如,如果我有X Y和Z并且我搜索了Z然后在过滤后我得到的位置必须是b 2因为它从0开始。这是我的自定义列表视图类:

public class MyCustomfilterAdapter extends BaseAdapter implements Filterable {

private List<String>originalData = null;
private List<String>filteredData = null;
private LayoutInflater mInflater;
private ItemFilter mFilter = new ItemFilter();

public MyCustomfilterAdapter(Context context, List<String> data) {
    this.filteredData = data ;
    this.originalData = data ;
    mInflater = LayoutInflater.from(context);
}

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

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

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

public View getView(int position, View convertView, ViewGroup parent) {
    // A ViewHolder keeps references to children views to avoid unnecessary calls
    // to findViewById() on each row.
    ViewHolder holder;

    // When convertView is not null, we can reuse it directly, there is no need
    // to reinflate it. We only inflate a new View when the convertView supplied
    // by ListView is null.
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.activity_customfilterlistview, null);

        // Creates a ViewHolder and store references to the two children views
        // we want to bind data to.
        holder = new ViewHolder();
        holder.text = (TextView) convertView.findViewById(R.id.lis2);

        // Bind the data efficiently with the holder.

        convertView.setTag(holder);
    } else {
        // Get the ViewHolder back to get fast access to the TextView
        // and the ImageView.
        holder = (ViewHolder) convertView.getTag();
    }

    // If weren't re-ordering this you could rely on what you set last time
    holder.text.setText(filteredData.get(position));

    return convertView;
}

static class ViewHolder {
    TextView text;
}

public Filter getFilter() {
    return mFilter;
}

private class ItemFilter extends Filter {
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {

        String filterString = constraint.toString().toLowerCase();

        FilterResults results = new FilterResults();

        final List<String> list = originalData;

        int count = list.size();
        final ArrayList<String> nlist = new ArrayList<String>(count);

        String filterableString ;

        for (int i = 0; i < count; i++) {
            filterableString = list.get(i);
            if (filterableString.toLowerCase().contains(filterString)) {
                nlist.add(filterableString);
            }
        }

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

        return results;
    }

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

}

这是我使用此自定义适配器的类:

  final MyCustomfilterAdapter adapter = new MyCustomfilterAdapter(Searchresults.this,lsts);
    search.setAdapter(adapter);


    inputSearch.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            System.out.println("Text ["+s+"]");

            adapter.getFilter().filter(s.toString());
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {

        }

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

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


           //What should i do here?
        }
    });

我想做的就是获取原始列表视图位置,即使在过滤我的结果后执行进一步操作但我被卡住只是因为我不知道如何摆脱这种情况。提前致谢。

1 个答案:

答案 0 :(得分:0)

您应该编写简单的类,它具有项目的名称和唯一的位置(您可以使用String而不是整数)。例如:

public class MyClass {
    private String name;
    private int originalPosition;
    public MyClass(String name, int originalPosition) {
        this.name = name;
        this.originalPosition = originalPosition;
    }
    private void getName() {
        return this.name;
    }
    private void getPosition() {
        return this.originalPosition;
    }
}

然后你应该在适配器上使用你的新类而不是字符串:

List<MyClass> items; // initialize somewhere
adapter = new MyCustomfilterAdapter(this, items); // Create the adapter to convert the array to views

然后,点击:

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    MyClass item = adapter.getItem(i);
    Log.d("myapp", "item name: " + item.getName(); // writes your item's name on Log
}