Android:AutoCompleteTextView 适配器在 onItemClick 方法中为空

发布于 2024-11-27 11:22:10 字数 2862 浏览 1 评论 0原文

正如标题所示,我有一个 AutoCompleteTextView,我在 onTextChanged 方法中填充它。我有一个扩展 ArrayAdapter 的自定义数组。我已将 onItemClickListener 添加到 AutoCompleteTextView,并且在 onItemClick 方法中,如果我执行“parent.getAdapter().getItem(position) - 它会引发错误,因为项目未填充在适配器中。

提供了相关代码:

public void onCreate(Bundle icicle){
    super.onCreate(icicle);
    thisContext = this;
    setContentView(R.layout.redeemareavouchers);

    // SUBURB SEARCH (AUTOCOMPLETE)
    txtSuburbSearch = (AutoCompleteTextView) findViewById(R.id.txtSearchSuburb);
    txtSuburbSearch.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            try{
                //getSuburbSearch returns results from backend
                PostalCodeDTO[] suburbs = client.getSuburbSearch(txtSuburbSearch.getText().toString(), 5);
                txtSuburbSearch.setAdapter(new SuburbAdapter(thisContext, R.layout.suburbitem, Arrays.asList(suburbs)));
            } catch (IOException ioe){ }
        }

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

        @Override
        public void afterTextChanged(Editable s) { }
    });

    txtSuburbSearch.setOnItemClickListener(new OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            try{
                Object test =parent.getAdapter().getItem(position);
                if (((SuburbAdapter)parent.getAdapter()).getItem(position) != null){
                    //I NEED TO GET HERE!!:)
                }
            } catch (Exception e){
                e.printStackTrace();
            }
        }
    });
}

和我的郊区适配器:

public class SuburbAdapter extends ArrayAdapter<PostalCodeDTO>{
    private final List<PostalCodeDTO> items;

    public SuburbAdapter(Context context, int textViewResourceId, List<PostalCodeDTO> items){
        super(context, textViewResourceId, items);
        this.items = items;
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        View v = convertView;
        if (v == null){
            LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.suburbitem, null);
        }

        PostalCodeDTO suburbs = items.get(position);
        if (suburbs != null){
            TextView sub = (TextView) v.findViewById(R.id.itemSuburb);

            if (sub != null){
                sub.setText(suburbs.toString());
            }
        }

        return v;
    }

    @Override
    public PostalCodeDTO getItem(int position){
        return items.get(position);
    }
}

So as the title suggests, I have an AutoCompleteTextView which I populate in the onTextChanged method. I have a custom array that extends ArrayAdapter. I've added an onItemClickListener to the AutoCompleteTextView, and in the onItemClick method, if i do a "parent.getAdapter().getItem(position) - it throws an error because the items are not populated in the adapter.

Relevant code provided:

public void onCreate(Bundle icicle){
    super.onCreate(icicle);
    thisContext = this;
    setContentView(R.layout.redeemareavouchers);

    // SUBURB SEARCH (AUTOCOMPLETE)
    txtSuburbSearch = (AutoCompleteTextView) findViewById(R.id.txtSearchSuburb);
    txtSuburbSearch.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            try{
                //getSuburbSearch returns results from backend
                PostalCodeDTO[] suburbs = client.getSuburbSearch(txtSuburbSearch.getText().toString(), 5);
                txtSuburbSearch.setAdapter(new SuburbAdapter(thisContext, R.layout.suburbitem, Arrays.asList(suburbs)));
            } catch (IOException ioe){ }
        }

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

        @Override
        public void afterTextChanged(Editable s) { }
    });

    txtSuburbSearch.setOnItemClickListener(new OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            try{
                Object test =parent.getAdapter().getItem(position);
                if (((SuburbAdapter)parent.getAdapter()).getItem(position) != null){
                    //I NEED TO GET HERE!!:)
                }
            } catch (Exception e){
                e.printStackTrace();
            }
        }
    });
}

and my SuburbAdapter:

public class SuburbAdapter extends ArrayAdapter<PostalCodeDTO>{
    private final List<PostalCodeDTO> items;

    public SuburbAdapter(Context context, int textViewResourceId, List<PostalCodeDTO> items){
        super(context, textViewResourceId, items);
        this.items = items;
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        View v = convertView;
        if (v == null){
            LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.suburbitem, null);
        }

        PostalCodeDTO suburbs = items.get(position);
        if (suburbs != null){
            TextView sub = (TextView) v.findViewById(R.id.itemSuburb);

            if (sub != null){
                sub.setText(suburbs.toString());
            }
        }

        return v;
    }

    @Override
    public PostalCodeDTO getItem(int position){
        return items.get(position);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文