【问题标题】:How to get exact index of selected item from filtered adapter?如何从过滤的适配器中获取所选项目的准确索引?
【发布时间】:2016-04-24 22:13:12
【问题描述】:

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

例如,我键入了“某物”和 textview 中列出的值。我的问题是从这一点开始。当我触发 onItemClick 时,我想获得准确的位置。但我返回过滤列表中的索引。

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

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();
}

}

【问题讨论】:

    标签: java android


    【解决方案1】:
    1. BasShape.java 中覆盖 equalshashcode
    2. AutoCompletePlaceAdapter 中保留对 originalList 的引用
    3. 调用 originalList.indexOf(clickedFilteredBasShape) 以检索原始 BasShape 索引 ...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-03
      • 2017-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-11
      相关资源
      最近更新 更多