Android的定制AutoCompleteTextView与自定义适配器(Android Cust

2019-10-18 17:28发布

基本上,我希望在EditText上写东西,然后一个网页的HTTP请求将被调用,它返回一个JSONObject其中包含一个JSON数组,其中包含在它里面的地方的价值观。 我需要populat随从JSON对象的结果autocompletetextview下拉列表。

我可以做的第二位,也就是说我可以填充我需要通过扩展arrayadapter如u可以看到下面的自定义适配器类值的下拉列表。 我的问题是第一位的,我怎么能覆盖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;
    }

}

Answer 1:

在PersonAdapter覆盖用getFilter()方法返回您在这里你必须实现它的两个方法的定制内部过滤器:performFiltering()和publishResults(),performFiltering在工作线程运行,并在这里你打电话给你的网络请求,在publishResults只是通话清晰(),并添加()项目



文章来源: Android Custom AutoCompleteTextView with Custom Adapter