Android:AutoCompleteTextView 适配器在 onItemClick 方法中为空
正如标题所示,我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论