Android 如何为AutoCompleteTextView实现HashMap

Android 如何为AutoCompleteTextView实现HashMap,android,autocomplete,hashmap,Android,Autocomplete,Hashmap,你好 我有以下问题。在我的Android应用程序中,我有一个包含公交站点名称和ID的XML条目列表。这些条目放在HashMap中,因为ID是唯一的,而站点名称不是唯一的。活动的用户界面包含一个AutoCompleteTextView和微调器 我的目标是使用站点名称填充自动完成视图,然后将所选站点的ID传递给另一个类,该类将在spinner中显示该站点上的总线线路(通过远程API) 因此,用户将要做的是开始键入stop name(例如Awesome stop),他将在自动完成建议中看到两个条目。根

你好

我有以下问题。在我的Android应用程序中,我有一个包含公交站点名称和ID的XML条目列表。这些条目放在
HashMap
中,因为ID是唯一的,而站点名称不是唯一的。活动的用户界面包含一个
AutoCompleteTextView
和微调器

我的目标是使用站点名称填充自动完成视图,然后将所选站点的ID传递给另一个类,该类将在spinner中显示该站点上的总线线路(通过远程API)

因此,用户将要做的是开始键入stop name(例如Awesome stop),他将在自动完成建议中看到两个条目。根据选择的微调器,微调器将显示不同的结果

我的问题是我不知道如何将AutoCompleteTextView和HashMap结合起来。通过
ArrayList
填充的
ArrayAdapter
可以很好地自动完成,但是它没有太大的帮助,因为我只能取回stop name,而且它没有太大的帮助,因为我实际上需要ID


非常感谢您提供的任何提示。

您使用的是
AutoCompleteTextView
而不是
MultiAutoCompleteTextView

MultiAutoCompleteTextView
正是您所需要的,因为它与
AutoCompleteTextView
完全相同,不同之处在于它可以显示多个建议(如果存在),并允许用户仅选择其中一个

参考图像:


关于一个很好的例子,请查看Android开发者:

好的,多亏了joaquin的提示,我花了一段时间才弄明白。这确实是通过实现自定义适配器实现的。而且
HashMap
对最初的目标没有多大帮助。下面是代码,如果有人遇到类似的挑战

活动:

// Members
private long currentStopId;
protected Map<String,String> lineMap;
protected ArrayList<Stop> stopMap;
protected String previousUrl = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_line);

    // Get the list of stops from resourse XML
    getStopInformation();

    // Enable auto-complete
    stopInput = (AutoCompleteTextView) findViewById(R.id.inputStopName);
    final HashMapAdapter adapter = new HashMapAdapter(this,R.layout.stop_list,stopMap);
    stopInput.setAdapter(adapter);

    // Add listener for auto-complete selection
    stopInput.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
            String selection = (String)parent.getItemAtPosition(position);

            // There we get the ID.
            currentStopId = parent.getItemIdAtPosition(position);
        }
    });
}
//成员
私有长currentStopId;
保护地图线图;
受保护的ArrayList叠图;
受保护字符串previousUrl=null;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u add\u行);
//从资源XML获取站点列表
getStopInformation();
//启用自动完成
stopInput=(AutoCompleteTextView)findViewById(R.id.inputStopName);
final HashMapAdapter=新的HashMapAdapter(this,R.layout.stop_列表,stopMap);
setAdapter(适配器);
//添加自动完成选择的侦听器
stopInput.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView父级、视图视图、整型位置、长rowId){
字符串选择=(字符串)parent.getItemAtPosition(位置);
//我们拿到身份证了。
currentStopId=parent.GetItemIDataPosition(位置);
}
});
}
适配器实现:

public class StopAdapter extends BaseAdapter implements Filterable {

private ArrayList<Stop> inputStopList;
private ArrayList<Stop> inputStopListClone;
private LayoutInflater lInflater;

/** Constructor */
public StopAdapter(Context context, int textViewResourceId, ArrayList<Stop> input) {
    lInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inputStopList = input;
    inputStopListClone = inputStopList;
}

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

@Override
public Object getItem(int i) {
    Stop value = inputStopList.get(i);
    return value.getStopName();
}

@Override
public long getItemId(int i) {
    Stop stop = inputStopList.get(i);
    long value = Long.parseLong(stop.getStopCode());
    return value;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    View myView = view;

    // R.layout.stop_list created in res/layout
    if(myView == null)
        myView = lInflater.inflate(R.layout.stop_list,viewGroup, false);

    ((TextView) myView.findViewById(R.id.textView)).setText(getItem(i).toString());

    return myView;
}

/** Required by AutoCompleteTextView to filter suggestions. */
@Override
public Filter getFilter() {
    Filter filter = new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence charSequence) {
            FilterResults filterResults = new FilterResults();

            if(charSequence == null || charSequence.length() == 0) {
                ArrayList<Stop> originalValues = new ArrayList<Stop>(inputStopList);
                filterResults.count = originalValues.size();
                filterResults.values = originalValues;
            }
            else {
                ArrayList<Stop> newValues = new ArrayList<Stop>();

                // Note the clone - original list gets stripped
                for(Stop stop : inputStopListClone)
                {
                    String lowercase = stop.getStopName().toLowerCase();
                    if(lowercase.startsWith(charSequence.toString().toLowerCase()))
                        newValues.add(stop);
                }

                filterResults.count = newValues.size();
                filterResults.values = newValues;
            }
            return filterResults;
        }

        @Override
        protected void publishResults(CharSequence charSequence, FilterResults filterResults) {

            if(filterResults != null && filterResults.count > 0) {
                inputStopList = (ArrayList<Stop>)filterResults.values;
                notifyDataSetChanged();
            }
            else notifyDataSetInvalidated();
        }
    };
    return filter;
}
公共类StopAdapter扩展BaseAdapter实现可过滤{
私有ArrayList inputStopList;
私有ArrayList inputStopListClone;
私人停车场;
/**建造师*/
公共StopAdapter(上下文上下文、int textViewResourceId、ArrayList输入){
lInflater=(LayoutInflater)context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE);
inputStopList=输入;
inputStopListClone=inputStopList;
}
@凌驾
public int getCount(){
返回inputStopList.size();
}
@凌驾
公共对象getItem(int i){
停止值=输入stoplist.get(i);
返回值。getStopName();
}
@凌驾
公共长getItemId(int i){
停止=输入stoplist.get(i);
long value=long.parseLong(stop.getStopCode());
返回值;
}
@凌驾
公共视图getView(int i、视图视图、视图组视图组){
视图myView=视图;
//R.layout.stop\u在res/layout中创建的列表
if(myView==null)
myView=lInflater.flate(右布局.停止列表,视图组,false);
((TextView)myView.findViewById(R.id.TextView)).setText(getItem(i).toString());
返回myView;
}
/**AutoCompleteTextView要求筛选建议*/
@凌驾
公共过滤器getFilter(){
过滤器过滤器=新过滤器(){
@凌驾
受保护过滤器结果执行过滤(CharSequence CharSequence){
FilterResults FilterResults=新的FilterResults();
if(charSequence==null | | charSequence.length()==0){
ArrayList originalValues=新的ArrayList(inputStopList);
filterResults.count=原始值.size();
filterResults.values=原始值;
}
否则{
ArrayList newValues=新的ArrayList();
//注意:克隆-原始列表被剥离
for(停止:inputStopListClone)
{
String lowercase=stop.getStopName().toLowerCase();
if(lowercase.startsWith(charSequence.toString().toLowerCase())
newValues.add(停止);
}
filterResults.count=newValues.size();
filterResults.values=新值;
}
返回过滤器结果;
}
@凌驾
受保护的无效发布结果(CharSequence CharSequence、FilterResults FilterResults){
如果(filterResults!=null&&filterResults.count>0){
inputStopList=(ArrayList)filterResults.values;
notifyDataSetChanged();
}
else NOTIFYDATESETINVALIDATED();
}
};
回流过滤器;
}

}

谢谢。我不太确定我是否有能力实现这种功能,但我一定会尝试:)我删除了我的评论,因为它是错误的。看看我的答案。我不确定我是否需要这里的
MultiAutoCompleteTextView
,因为我最终只需要选择一个站点,而不是多个站点。据我所知,两者的区别在于能够继续自动完成多个任务