2016-04-24 41 views
0

我想開發Android應用程序,我使用AutoCompleteTextView與自定義適配器。我正在向用戶展示位置。用戶可以輸入textview來查找目標文本。我在MainActivity中將onItemClickListener添加到autocompletetextview並將數據填充到數組適配器。
如何從過濾適配器所選項目的具體指標?

比如我輸入「東西」和價值觀上的TextView上市。我的問題是從這一點開始。當我觸發onItemClick時,我想獲得確切位置。但是我返回的是過濾列表中的索引。

如何獲得所選項目的exect索引(在適配器中)?

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

}

回答

0
  1. 覆蓋等於哈希碼BasShape.java
  2. 請參考originalListAutoCompletePlaceAdapter
  3. 呼叫originalList.indexOf(clickedFilteredBasShape)找回原來BasShape指數...
相關問題