【问题标题】:Android Custom AutoCompleteTextView with Custom Adapter带有自定义适配器的 Android 自定义 AutoCompleteTextView
【发布时间】:2013-10-07 12:27:58
【问题描述】:

基本上,我希望在edittext中写一些东西,然后将调用一个Web http请求,该请求返回一个JSONObject,其中包含一个JSON数组,其中包含其中某处的值。我需要使用 JSON 对象的结果填充 autocompletetextview 附带的下拉列表。

我可以做第二点,即我可以通过使用扩展 arrayadapter 的自定义适配器类来使用我需要的值填充下拉列表,如下所示。我的问题是第一个问题,如何覆盖 AutoCompleteTextView 使其不显示从数组中过滤的常量值,而是显示我给它的值?我根本不希望它是可过滤的。这是autocompletetextview的源代码http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.4_r2.1/android/widget/AutoCompleteTextView.java#91

public class PersonAdapter extends ArrayAdapter
{

    // we use the constructor allowing to provide a List of objects for the data
    // to be binded.
    public PersonAdapter(Context context, int textViewResourceId,
            List objects) {
        super(context, textViewResourceId, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        // retrieve the Person object binded at this position
        final Person p = getItem(position);

        // A ViewHolder keeps references to children views to avoid unneccessary
        // calls
        // to findViewById() on each row.
        ViewHolder holder;

        // When convertView is not null, we can reuse it directly, there is no
        // need
        // to reinflate it. We only inflate a new View when the convertView
        // supplied
        // by ListView is null.
        if (convertView == null) {
            convertView = View.inflate(getContext(), R.layout.list_item, parent, false);

            // Creates a ViewHolder and store references to the two children
            // views
            // we want to bind data to.
            holder = new ViewHolder();
            holder.textName = (TextView) convertView
                    .findViewById(R.id.textName);
            holder.textEmail = (TextView) convertView
                    .findViewById(R.id.textEmail);
            holder.picture = (ImageView) convertView.findViewById(R.id.image);
            holder.picture.setFocusable(false);
            holder.picture.setFocusableInTouchMode(false);
            convertView.setTag(holder);
        } else {
            // Get the ViewHolder back to get fast access to the TextView
            // and the ImageView.
            holder = (ViewHolder) convertView.getTag();
        }

        // Bind the data efficiently with the holder.
        holder.textName.setText(p.getName());
        holder.textEmail.setText(p.getEmail());
        holder.picture.setImageResource(p.getResImage());
                //click on the picture
        holder.picture.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getContext(),
                        "Clicked on " + p.getName() + "'s picture",
                        Toast.LENGTH_SHORT).show();

            }
        });

        return convertView;
    }

    /**
     *
     * Inner holder class for a single row view in the ListView
     *
     */
    static class ViewHolder {
        TextView textName, textEmail;
        ImageView picture;
    }

}

【问题讨论】:

    标签: java android autocomplete android-arrayadapter autocompletetextview


    【解决方案1】:

    在您的 PersonAdapter 覆盖 getFilter() 方法中返回您的自定义内部过滤器,您必须在其中实现它的两个方法:performFiltering() 和 publishResults(),performFiltering 在工作线程中运行,在这里您在 publishResults 中调用您的 Web 请求只需调用 clear() 和 add() 项目

    【讨论】:

    猜你喜欢
    • 2023-03-19
    • 1970-01-01
    • 2021-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多