具有自定义ArrayAdapter实现的AutoCompleteTextView未显示视图列表

时间:2016-07-15 15:55:38

标签: android android-arrayadapter autocompletetextview

我正在尝试使用扩展ArrayAdapter的自定义适配器实现autocompletetextview。问题是正在调用适配器的getView,它表明数据也是正确的。但是,当我在ACTextView中输入内容时,视图不会显示。 感谢任何帮助,谢谢!

public class NewEventActivity extends AppCompatActivity {
    MemNameAdapter memNameAdapter;
    PurposeAdapter purposeAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_event);
        ButterKnife.bind(this);

        memNameAdapter = new MemNameAdapter(this, R.layout.simple_text_view, new ArrayList<Member>());
        memNameAdapter.setDropDownViewResource(R.layout.simple_text_view);
        memNameAdapter.setNotifyOnChange(true);

        txtMemName.setAdapter(memNameAdapter);
        txtMemName.addTextChangedListener(memNmTxtWatcher);

        txtMemName.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                eMember = (Member) parent.getSelectedItem();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                eMember = null;
            }
        });        

    }

    TextWatcher memNmTxtWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            ArrayList<Member> members = eGroup.getMembers();
            if (members != null) {
                memNameAdapter.setRecords(members);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };

    class MemNameAdapter extends ArrayAdapter<Member> {
        Context context;
        int resource;
        ArrayList<Member> records;

        public MemNameAdapter(Context context, int resource, ArrayList<Member> records) {
            super(context, resource, records);
            this.context = context;
            this.resource = resource;
            this.records = records;
        }

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

        public void setRecords(ArrayList<Member> records) {
            this.records.clear();
            this.records.addAll(records);
            notifyDataSetChanged();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = convertView;
            if (view == null) {
                view = LayoutInflater.from(context).inflate(resource, parent, false);
            }
            Log.d(TAG, "GetView: " + records.get(position).getMemberName() + ", " + view);
            ((TextView) view).setText(records.get(position).getMemberName());
            return view;
        }

    }

}

0 个答案:

没有答案