如何为 AutoCompleteTextView 实现 HashMap

How to implement HashMap for AutoCompleteTextView

美好的一天,

我有以下问题。在我的 Android 应用程序中,我在 XML 中有一个条目列表,其中包含公交车站名称和 ID。这些放在 HashMap 中,因为 ID 是唯一的,而停止名称不是。 activity 的用户界面包含一个 AutoCompleteTextView 和 Spinner。

我的objective是用stop names填充自动完成视图,然后传递[=35的ID =]ed 停止到另一个 class,它将在微调器中显示该站点的公交线路(通过远程 API)。

所以用户要做的是开始输入站点名称(例如 Awesome Stop),他将在自动完成建议中看到两个条目。 select 微调器会显示不同的结果。

我的问题是我不知道如何结合 AutoCompleteTextView 和 HashMap。自动完成与通过 ArrayList<String> 填充的 ArrayAdapter<String> 配合得很好,但它不是很有用,因为我只能取回停止名称,而且它不是很有用,因为我实际上需要 ID。

非常感谢任何提示。

您正在使用 AutoCompleteTextView 而不是 MultiAutoCompleteTextView

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

参考图片:http://i.stack.imgur.com/9wMAv.png

查看 Android 开发人员的示例:http://developer.android.com/reference/android/widget/MultiAutoCompleteTextView.html

好的,经过相当长的一段时间我弄明白了,多亏了 joaquin 的提示。它确实是通过实现自定义适配器来完成的。 HashMap 对最初的目标帮助不大。如果有人遇到类似的挑战,下面是代码。

Activity:

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

适配器实现:

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

}