如何创建自定义的BaseAdapter的AutoCompleteTextView [英] How to create custom BaseAdapter for AutoCompleteTextView

查看:198
本文介绍了如何创建自定义的BaseAdapter的AutoCompleteTextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直有困难,创​​建自定义ArrayAdapter为AutoCompleteTextView这样的错误,会拿出尽管在互联网上找到将以下code:

I've been having difficulty creating a custom ArrayAdapter for AutoCompleteTextView such errors that would come up despite following code found on the internet would be:

  • 在下拉列表将不会出现。
  • 在自定义对象及其详细信息将不会出现。

因此​​,对于那些谁是有还是有同样的问题,我建议使用BaseAdapter的AutoCompleteTextView代替。

So for those who are having or had the same problem as me, I recommend using BaseAdapter for AutoCompleteTextView instead.

推荐答案

自定义BaseAdapter类

Custom BaseAdapter Class

public class ObjectAdapter extends BaseAdapter implements Filterable {

    private Context context;
    private ArrayList<Object> originalList;
    private ArrayList<Object> suggestions = new ArrayList<>();
    private Filter filter = new CustomFilter();

    /**
     * @param context      Context
     * @param originalList Original list used to compare in constraints.
     */
    public ObjectAdapter(Context context, ArrayList<Object> originalList) {
        this.context = context;
        this.originalList = originalList;
    }

    @Override
    public int getCount() {
        return suggestions.size(); // Return the size of the suggestions list.
    }

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


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

    /**
     * This is where you inflate the layout and also where you set what you want to display.
     * Here we also implement a View Holder in order to recycle the views.
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(context);

        ViewHolder holder;

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.adapter_autotext,
                    parent,
                    false);
            holder = new ViewHolder();
            holder.autoText = (TextView) convertView.findViewById(R.id.autoText);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.autoText.setText(suggestions.get(position).getCountryName());

        return convertView;
    }


    @Override
    public Filter getFilter() {
        return filter;
    }

    private static class ViewHolder {
        TextView autoText;
    }

    /**
     * Our Custom Filter Class.
     */
    private class CustomFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            suggestions.clear();

            if (originalList != null && constraint != null) { // Check if the Original List and Constraint aren't null.
                for (int i = 0; i < originalList.size(); i++) {
                    if (originalList.get(i).getCountryName().toLowerCase().contains(constraint)) { // Compare item in original list if it contains constraints.
                        suggestions.add(originalList.get(i)); // If TRUE add item in Suggestions.
                    }
                }
            }
            FilterResults results = new FilterResults(); // Create new Filter Results and return this to publishResults;
            results.values = suggestions;
            results.count = suggestions.size();

            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            if (results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }
    }
}

主要活动类

public class MainActivity extends AppCompatActivity{

    private SGetCountryListAdapter countryAdapter;
    private ArrayList<SGetCountryList> countryList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        country = (AutoCompleteTextView) findViewById(R.id.country);
        countryAdapter = new SGetCountryListAdapter(getApplicationContext(),
                    ConnectionParser.SGetCountryList);

        country.setAdapter(countryAdapter);
        country.setThreshold(1);

    }

}

下拉布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/autoText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:textColor="@color/black" />

</LinearLayout>

我原来的名单已经从Web服务获取的数据让我们姑且认为它已经拥有的数据。当然,你更可以通过添加更多的观点自定义下拉列表中,只是不要忘了,以纳入新的意见更新适配器。

My Original List has data taken from web service so let's just assume that it already has data. Of course you can customize the dropdown even more by adding more views, just don't forget to update the adapter in order to incorporate the new views.

这篇关于如何创建自定义的BaseAdapter的AutoCompleteTextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆