java - Android 自定义 AutoCompleteTextView 与自定义适配器

标签 java android autocomplete android-arrayadapter autocompletetextview

基本上,我希望在编辑文本中写一些东西,然后将调用一个 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;
    }

}

最佳答案

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

关于java - Android 自定义 AutoCompleteTextView 与自定义适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19114602/

相关文章:

java - Spring JPA - 具有该位置 [1] 的参数不存在

java - 部署到本地 Tomcat 的 Web 应用程序的 Apache Ant + Ivy 构建

Android Q-municate 错误无法解决 : com. efollestad :material-dialog:0. 7.8.1

java - 缩放 3D 对象会产生奇怪的结果

Android 在对话框工作时更改 ProgressDialog 可绘制

javascript - 如何在 JQuery UI 自动完成中显示名称并获取 id?

c++ - 使用 Visual Studios 2012 进行 C++ 编码时,如何自动完成在 IntelliSense 中选择的代码?

Java - 从用户获取特定输入的有效方法

java - 使用Java递归列出目录中的所有文件

vim - 如何有效地撤消vim中的自动完成