如何从过滤后的适配器获取所选项目的确切索引?

时间:2016-04-24 22:13:12

标签: java android

我正在尝试开发Android应用程序,我正在使用自定义适配器的AutoCompleteTextView。我正在向用户展示地点。用户可以键入textview以查找目​​标文本。我将onItemClickListener添加到MainActivity中的autocompletetextview并将数据填充到数组适配器。

例如,我在textview上输入了'something'和值。我的问题是从这一点开始。当我触发onItemClick时,我想获得准确的位置。但是我返回了filterd列表中的索引。

如何获取所选项目的索引(在适配器中)?

BasShape.java

public class BaseShape {

private int id;
private String text;
private Rect coordinate;

public BaseShape(int id, String text, Rect coordinate) {
    this.id = id;
    this.text = text;
    this.coordinate = coordinate;
}

@Override
public String toString() {
    return text;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getText() {
    return text;
}

public void setText(String text) {
    this.text = text;
}

public Rect getCoordinate() {
    return coordinate;
}

public void setCoordinate(Rect coordinate) {
    this.coordinate = coordinate;
}

}
AutoCompletePlaceAdapter.java

public class AutoCompletePlaceAdapter extends ArrayAdapter<BaseShape> {

private final List<BaseShape> places;
public List<BaseShape> filteredPlaces = new ArrayList<BaseShape>();

public AutoCompletePlaceAdapter(Context context, List<BaseShape> places) {
    super(context, 0, places);
    this.places = places;
}

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

@Override
public Filter getFilter() {
    return new PlacesFilter(this, places);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Get the data item from filtered list.
    BaseShape place = (BaseShape) filteredPlaces.get(position);

    // Inflate your custom row layout as usual.
    LayoutInflater inflater = LayoutInflater.from(getContext());
    convertView = inflater.inflate(R.layout.row_places, parent, false);

    TextView tvName = (TextView) convertView.findViewById(R.id.textViewPlaceName);
    ImageView ivIcon = (ImageView) convertView.findViewById(R.id.imageViewPlaceImage);
    tvName.setText(place.getText());
    ivIcon.setImageResource(R.drawable.save);

    return convertView;
}

}
和PlacesFilter.java

public class PlacesFilter extends Filter {

AutoCompletePlaceAdapter adapter;
List<BaseShape> originalList;
List<BaseShape> filteredList;

public PlacesFilter(AutoCompletePlaceAdapter adapter, List<BaseShape> originalList) {
    super();
    this.adapter = adapter;
    this.originalList = originalList;
    this.filteredList = new ArrayList<>();
}

@Override
protected FilterResults performFiltering(CharSequence constraint) {
    filteredList.clear();
    final FilterResults results = new FilterResults();

    if (constraint == null || constraint.length() == 0) {
        filteredList.addAll(originalList);
    } else {
        final String filterPattern = constraint.toString().toLowerCase().trim();

        // Your filtering logic goes in here
        for (final BaseShape shape: originalList) {
            if (shape.getText().toLowerCase().contains(filterPattern)) {
                filteredList.add(shape);
            }
        }
    }
    results.values = filteredList;
    results.count = filteredList.size();
    return results;
}

@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
    adapter.filteredPlaces.clear();
    adapter.filteredPlaces.addAll((List) results.values);
    adapter.notifyDataSetChanged();
}

}

1 个答案:

答案 0 :(得分:0)

  1. BasShape.java
  2. 中覆盖等于哈希码
  3. AutoCompletePlaceAdapter
  4. 中保留对 originalList 的引用
  5. 调用 originalList.indexOf(clickedFilteredBasShape)以检索原始 BasShape 索引......