
自动完成文本正确列出所有内容都可以正常工作..但是问题是,一旦我单击自动完成项,它就没有选择.
当我单击项目时,Logcat显示“未选择项目”.
autocomplte.setonItemClickListener(new OnItemClickListener() { @OverrIDe public voID onItemClick(AdapterVIEw<?> arg0, VIEw arg1, int position, long arg3) { // Log.d("your selected item",""+s1.get(position)); System.out.println("sajdksadkasjdksajdksa"); // s1.get(position) is name selected from autocompletetextvIEw // Now you can show the value on textvIEw. } });// ————– Adapter类
public class LoadLocationBasedonCountry extends BaseAdapter implements Filterable {private ArrayList<CountryLocation> locList;private ArrayList<CountryLocation> storedLocList;private Activity mActivity;private LayoutInflater inflater;public LoadLocationBasedonCountry(Activity activity, ArrayList<CountryLocation> joes) { this.mActivity = activity; this.locList = joes; storedLocList = new ArrayList<CountryLocation>(joes);}@OverrIDepublic VIEw getVIEw(final int position, VIEw convertVIEw, VIEwGroup parent) { final VIEwHolder holder; VIEw vi = convertVIEw; if (vi == null) { holder = new VIEwHolder(); inflater = (LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); vi = inflater.inflate(R.layout.row_loc_based_on_country, null,false); holder.txtCtyname = (TextVIEw) vi.findVIEwByID(R.ID.txtCtyname); vi.setTag(holder); } else { holder = (VIEwHolder) vi.getTag(); } CountryLocation cLoc = locList.get(position); holder.txtCtyname.setText(cLoc.getLocname() + ", " + cLoc.getCountry()); return vi;}@OverrIDepublic int getCount() { return locList.size();}@OverrIDepublic Object getItem(int position) { return null;}@OverrIDepublic long getItemID(int position) { return 0;}public static class VIEwHolder { private TextVIEw txtCtyname;}@OverrIDepublic Filter getFilter() { Filter filter = new Filter() { @SuppressWarnings("unchecked") @OverrIDe protected voID publishResults(CharSequence constraint,FilterResults results) { locList = (ArrayList<CountryLocation>) results.values; notifyDataSetChanged(); } @OverrIDe protected FilterResults performFiltering(CharSequence constraint) { FilterResults results = new FilterResults(); ArrayList<CountryLocation> FilteredArrList = new ArrayList<CountryLocation>(); if (constraint == null || constraint.length() == 0) { results.count = storedLocList.size(); results.values = storedLocList; } else { constraint = constraint.toString(); for (int i = 0; i < storedLocList.size(); i++) { CountryLocation country = storedLocList.get(i); if (country.getLocname() .tolowerCase(Locale.getDefault()) .startsWith(constraint.toString())) { FilteredArrList.add(country); } } results.count = FilteredArrList.size(); results.values = FilteredArrList; } return results; } }; return filter;}public voID updateList(ArrayList<CountryLocation> List) { locList.clear(); locList = List; notifyDataSetChanged();}}
有人遇到过这类问题吗?
解决方法:
终于我找到了解决方案;
由于在autocompleteTextvIEw中,它通过使用方法“ performCompletion”的adapter.getItem(position)获得值—–请参阅androID.Widget.autoCompleteTextvIEw
样品:
private voID performCompletion(VIEw selectedVIEw, int position, long ID) { if (isPopupShowing()) { Object selectedItem; if (position < 0) { selectedItem = mPopup.getSelectedItem(); } else {selectedItem = mAdapter.getItem(position); //如果我们从适配器返回null,则此行将为null
} if (selectedItem == null) { Log.w(TAG, "performCompletion: no selected item"); return; } @OverrIDepublic Object getItem(int position) {return null;}所以正确的方法是
@OverrIDe public Object getItem(int position){ return locList.get(position).getLocname();} 总结 以上是内存溢出为你收集整理的android-“未选择项目”,同时单击了AutoCompleteTextview项目全部内容,希望文章能够帮你解决android-“未选择项目”,同时单击了AutoCompleteTextview项目所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)