Question

Basically, I wish to write something in the edittext, then a web http request will be called that returns a JSONObject which contains a JSON array, which contain the values somewhere inside it. I need to populat the dropdown list that comes with autocompletetextview with the results from the JSON Object.

I can do the second bit, i.e. I can populate the dropdown list with the values I need by using a custom adapter class that extends arrayadapter as u can see below. My problem is with the first bit, how can I override AutoCompleteTextView such that it doesn't show me filtered constant values from an array, rather shows me the values that I give it ? I don't want it to be filterable at all. Here is the sourcecode for 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;
    }

}
Was it helpful?

Solution

in your PersonAdapter override getFilter() method returning your custom inner Filter where you have to implement its two methods: performFiltering() and publishResults(), performFiltering is run in the worker thread and here you call your web request, in publishResults just call clear() and add() the items

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top