无法从MultiAutoCompleteTextView中选择提示

时间:2018-03-26 09:52:31

标签: android

好的,这就是我如何设置我的MultiAutoCompleteTextView

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.itemprice_layout, container, false);
    mAutoCompleteTextView = v.findViewById(R.id.itemcode);
    mAutoCompleteTextView.setHint("ItemCode");
    mAutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
    mAutoCompleteTextView.setThreshold(3);
    mAutoCompleteTextView.addTextChangedListener(new TextWatcher(){
        public void afterTextChanged(Editable s) {
        }

        public void beforeTextChanged(CharSequence s, int start, int count,int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before,
                                  int count) {
            if (mAutoCompleteTextView.getText().toString().length() > 3) {
                SessionHandler ses = new SessionHandler();
                String outlet = ses.getOutletCode(getContext());
                String ItemInput = mAutoCompleteTextView.getText().toString();
                new getList().execute(ItemInput,outlet);
            }
        }

    });
    return v;
}

以下是dropdown

的屏幕展示

enter image description here

我的适配器

public class Itempriceadapter extends BaseAdapter  implements Filterable {

    private Context context;
    private LayoutInflater inflater;
    List<Itempricemodel> data = Collections.emptyList();
    private Filter filter = new CustomFilter();

    private List<Itempricemodel> mArrayList;
    private List<Itempricemodel> mFilteredList;

    public Itempriceadapter(Context context, List<Itempricemodel> data){
        this.context=context;
        inflater= LayoutInflater.from(context);
        this.data=data;
        mArrayList = new ArrayList<>();
        mFilteredList = new ArrayList<>();

        if(data != null) {
            mArrayList.addAll(data);
        }
    }


    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }
    @Override

    public int getCount() {
        return mArrayList.size();
    }
    @NonNull
    @Override
    public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View view              = inflater.inflate(R.layout.itempricedetail, parent,false);
        final TextView textviewitem  = view.findViewById(R.id.textviewitem);
        Itempricemodel current = data.get(position);
        textviewitem.setText(current.getItemCode() + " - " + current.getItemName());
        return  view;
    }
    @Override
    public Filter getFilter() {
        return filter;
    }
    private class CustomFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            mFilteredList.clear();
            if(mArrayList != null && constraint != null) {
                for(int i=0 ; i<mArrayList.size(); i++) {
                    if(mArrayList.get(i).getItemCode().toLowerCase().contains(constraint)) {
                        mFilteredList.add(mArrayList.get(i));
                    }
                }
            }
            FilterResults results = new FilterResults();
            results.values = mFilteredList;
            results.count = mFilteredList.size();
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            if(results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }
    }
}

所以,问题是当我点击下拉项时没有任何反应。我错过了什么 ?那么,如何做到这一点:当我点击该项目时,该项目将移动到MultiAutoCompleteTextView field。对不起我的英文

1 个答案:

答案 0 :(得分:0)

您必须设置OnItemSelected侦听器:

mAutoCompleteTextView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected (AdapterView<?> parent, View view, int position, long id) {
        //...
    }
})

请仔细阅读本文档:

https://developer.android.com/reference/android/widget/AutoCompleteTextView.html

修改

变化:

@Override
public Object getItem(int position) {
    return null;
}

要:

@Override
public Object getItem(int position) {
    return mArrayList.get(position);
}