如何为AutoCompleteTextView实现HashMap

时间:2015-01-15 10:54:46

标签: android autocomplete hashmap

美好的一天,

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

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

那么用户将要做的是开始输入停止名称(例如Awesome Stop),他将在自动完成建议中看到两个条目。根据他将选择哪一个,微调器将显示不同的结果。

我的问题是我无法弄清楚如何结合AutoCompleteTextView和HashMap。通过ArrayAdapter<String>填充的ArrayList<String>自动完成效果很好,但它不是非常有用,因为我只能获得停止名称,因为我实际上需要ID,所以它不是很有帮助

非常感谢任何提示。

2 个答案:

答案 0 :(得分:1)

好的,经过相当长的一段时间后,我想出来了,感谢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);
        }
    });
}

适配器实施:

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

}

答案 1 :(得分:0)

您使用AutoCompleteTextView代替MultiAutoCompleteTextView

MultiAutoCompleteTextView正是您所需要的,因为它与AutoCompleteTextView完全相同,区别在于它可以显示多个建议(如果存在),并且让用户只选择其中一个。

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

有关一个很好的示例,请查看Android开发者:http://developer.android.com/reference/android/widget/MultiAutoCompleteTextView.html

相关问题