当点击AutoCompleteTextView项目时,出现“未选择项目”的提示。

3
自动完成文本的列表显示正常,一切都运行良好。但是问题在于,一旦我点击自动完成项目,它就无法被选择。当我点击这些项目时,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.
        }
    });

//--------------适配器类

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

@Override
public 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;
}

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

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

public static class ViewHolder {
    private TextView txtCtyName;

}

@Override
public 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();
}

有人遇到过这种问题吗?


这里没完全理解...你的适配器类是什么?你的父布局/主视图的其他元素上是否有其他监听器? - mithrop
我正在使用一个AutoCompleteTextView来根据国家显示位置。根据搜索字符串,它从服务器加载正确的值。但问题是当我选择一个项目时,它没有做任何操作...并且logcat显示为"未选择项目"。 - KP_
好的。您正在设置一个OnItemClickListener的Autocomplete实例是什么? - mithrop
acWorkLocation.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) 是从自动完成文本视图中选择的名称 // 现在您可以在文本视图上显示该值。 } }); - KP_
我们需要实例的类型 ;) 实际上,我不知道是否能找到问题,但是在这里我甚至看不出它来自哪里。 - mithrop
使用自定义模型类设置自动完成文本视图而不是字符串会有任何问题吗? - KP_
1个回答

5

最终我找到了解决方案;

在自动完成文本框中,它使用适配器的getItem(position)方法获取值,使用"performCompletion"方法-----请参阅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); // 如果我们从适配器返回空,则此行将获取空值

}
        if (selectedItem == null) {
            Log.w(TAG, "performCompletion: no selected item");
            return;
        }

 @Override
public Object getItem(int position) {
return null;
}

因此正确的做法是

@Override
 public Object getItem(int position)
{
    return locList.get(position).getLocName();
}

非常感谢您!我一直在想为什么onItemClickListener根本没有被调用。getItem()必须已经实现了非空结果! - Randy Sugianto 'Yuku'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接