Tuesday, 3 November 2020

FIrst dropdown item to be fixed in autoCompleteTextView

I created an app that has an autoCompleteTextView in order to allow the user to perform search queries.

Once they start typing, a dropdown appears and offers the results.

enter image description here

Now, I would like to make the first item to be fixed and unscrollable which will say something like: can't find? add manually.

How can I make the first item in the suggested dropdown list to be fixed and appear always?

My code for the adapter is:

public class AutoCompleteImageAdapter extends ArrayAdapter<String> implements Filterable {

    private ArrayList<String> fullList;
    private ArrayList<String> mOriginalValues;
    private ArrayFilter mFilter;
    private Boolean noResults;
    private TextView tv_name;
    private ImageView im_cover;
    private List<String> url, id;

    private StorageReference storageRef;
    private FirebaseFirestore db;

    public AutoCompleteImageAdapter(Context context, int resource, int textViewResourceId, List<String> objects, List<String> url, List<String> id, Boolean noResult) {

        super( context, resource, textViewResourceId, objects );
        fullList = (ArrayList<String>) objects;
        mOriginalValues = new ArrayList<String>( fullList );
        noResults = noResult;
        this.url = url;
        this.id = id;
    }

    @Override
    public int getCount() {
        if (fullList.size() > 40) {
            return 40;
        } else {
            return fullList.size();
        }
    }

    @Override
    public String getItem(int position) {
        return fullList.get( position );
    }

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

        db = FirebaseFirestore.getInstance();
        storageRef = FirebaseStorage.getInstance().getReference();

        View row = convertView;

        String id = this.id.get( position );
        LayoutInflater inflater = LayoutInflater.from( getContext() );

        if (row == null) {
            row = inflater.inflate( R.layout.item_auto_add, parent, false );
        }

        tv_name = (TextView) row.findViewById( R.id.item_drop );
        tv_name.setText( fullList.get( position ) );

        im_cover = row.findViewById( R.id.iv_itemCover );
        String Url = url.get( position );
        if (id.length() > AppConstants.UPLOADED_item_LENGTH) {
            storageRef.child( "/itemCovers/" + Url + "/" + Url + ".jpg" ).getDownloadUrl().addOnSuccessListener( new OnSuccessListener<Uri>() {
                @Override
                public void onSuccess(Uri uri) {
                    Picasso.with( parent.getContext() ).load( uri ).resize( 110, 160 ).into( im_cover );
                }
            } ).addOnFailureListener( new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception exception) {
                    Log.d( "ERROR", exception + "" );
                }
            } );
        } else {
            Picasso.with( parent.getContext() ).load( Uri.parse( Url ) ).error( R.drawable.ic_nocover ).resize( 110, 160 ).into( im_cover );
        }


        return row;

    }

    @Override
    public Filter getFilter() {
        if (mFilter == null) {
            mFilter = new ArrayFilter();
        }
        return mFilter;
    }

    private class ArrayFilter extends Filter {
        private Object lock;

        @Override
        protected FilterResults performFiltering(CharSequence prefix) {
            FilterResults results = new FilterResults();

            if (mOriginalValues == null) {
                synchronized (lock) {
                    mOriginalValues = new ArrayList<String>( fullList );
                }
            }

            if (prefix == null || prefix.length() == 0) {
                synchronized (lock) {
                    ArrayList<String> list = new ArrayList<String>( mOriginalValues );
                    results.values = list;
                    results.count = list.size();
                }
            } else {
                final String prefixString = prefix.toString().toLowerCase();

                ArrayList<String> values = mOriginalValues;
                int count = values.size();

                ArrayList<String> newValues = new ArrayList<String>( count );

                for (int i = 0; i < count; i++) {
                    String item = values.get( i );
                    if (item.toLowerCase().contains( prefixString )) {
                        newValues.add( item );
                    }

                }

                results.values = newValues;
                results.count = newValues.size();
            }

            return results;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {

            if (results.values != null) {
                fullList = (ArrayList<String>) results.values;
            } else {
                fullList = new ArrayList<String>();
            }
            if (results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }
    }

    public void clear() {
        if (fullList != null) {
            fullList.clear();
            notifyDataSetChanged();
        }
    }
}

Thank you



from FIrst dropdown item to be fixed in autoCompleteTextView

No comments:

Post a Comment