开发者

Android: AutoCompleteTextView adapter empty in onItemClick method

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜