why autosuggest not display and how to add filter functionalty

帅比萌擦擦* 提交于 2019-12-25 18:45:12

问题


I make a simple demo of auto complete. I take array of string in which there is 2250 entries with Name and code like that example "Alexandra Palace-(AAP)", first name is given and the it code inside the bracket.my issue is I need to filter this using code not by name .In other word if i type anything in input field it filter with starting characters of element mean name I need to filter with code which is inside the bracket.

Code:

when I type "lwy" it will not show "MNCRLWY-(LWY)", can you please tell how I will achieve this ?

here is my code..

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.select_station);

 autocompleteView = (AutoCompleteTextView) findViewById(R.id.item_autoComplete);

 STATION_LIST = new String[GlobalList.stationList.length
                                    + GlobalExtendStationList.stationList.length];
                            System.arraycopy(GlobalList.stationList, 0, STATION_LIST, 0,
                                    GlobalList.stationList.length);
                            System.arraycopy(GlobalExtendStationList.stationList, 0,
                                    STATION_LIST, GlobalList.stationList.length,
                                    GlobalExtendStationList.stationList.length);
                            autosuggestAdapter = new CustomAutocompletAdapter(this,STATION_LIST);
                            autocompleteView.setAdapter(autosuggestAdapter);

customAutosuggestAdapter:

public class CustomAutocompletAdapter extends BaseAdapter implements Filterable{

    String[] autolistArray;
    private Context context;
    public CustomAutocompletAdapter( Context context, String[] autolistArray){
        this.autolistArray=autolistArray;
        this.context = context;
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
         View v = convertView;
            if (v == null) {
                LayoutInflater mInflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = mInflater.inflate(R.layout.custom_row_adapter, null);
            }

            final TextView stationNameAndCode = (TextView) v
                    .findViewById(R.id.item_selectStationName);



            final String stationNameAndCodeValue = autolistArray[position];


            stationNameAndCode.setText(stationNameAndCodeValue);


            return v;
    }

    @Override
    public Filter getFilter() {
        // TODO Auto-generated method stub
        Filter myFilter = new Filter() {

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

                System.out.println("Constraint " + constraint);
                Log.d("-----------", "publishResults");
                // has


            }

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                   Log.d("-----------", "performFiltering");
                FilterResults results = new FilterResults(); // Holds the
                                                                // results of a
                                                                // filtering
                                                                // operation in
                                                                // values



                /********
                 * 
                 * If constraint(CharSequence that is received) is null returns
                 * the mOriginalValues(Original) values else does the Filtering
                 * and returns FilteredArrList(Filtered)
                 * 
                 ********/

                Locale locale = Locale.getDefault();

                constraint = (String) constraint
                        .toString().toLowerCase(locale);
                if (constraint == null || constraint.length() == 0) {

                    // set the Original result to return

                } else {


                }
                return results;
            }

            @Override
            public CharSequence convertResultToString(Object resultValue) {
                // TODO Auto-generated method stub
                //convert object to string
                Log.d("-----------", "convertResultToString");
                return "";
            }
        };
        return myFilter;
    }

}

It is not showing no auto suggest. Could you please tell how I will achieve this? Mean filter functionality? any update of this Question


回答1:


try this,

public class CustomAutocompletAdapter extends BaseAdapter implements Filterable{

private  String stationNameAndCodeValue ;
ArrayList<String> autolistArray;
ArrayList<String> objects;
private Context context;
public CustomAutocompletAdapter( Context context, String[] autolistArray){
    this.autolistArray=new ArrayList<String>();
 for(int i=0;i<autolistArray.length;i++){


this.autolistArray.add(autolistArray[i]);

}        this.context = context;
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return autolistArray.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return autolistArray.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return  position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
     View v = convertView;
        if (v == null) {
            LayoutInflater mInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = mInflater.inflate(R.layout.custom_row_adapter, null);
        }

        final TextView stationNameAndCode = (TextView) v
                .findViewById(R.id.item_selectStationName);



        stationNameAndCodeValue = autolistArray.get(position);


        stationNameAndCode.setText(stationNameAndCodeValue);


        return v;
}

@Override
public Filter getFilter() {
    // TODO Auto-generated method stub
    Filter myFilter = new Filter() {

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

            System.out.println("Constraint " + constraint);
            Log.d("-----------", "publishResults");
              if (results.count > 0 && results != null) {
            objects = (ArrayList<String>) results.values;

            notifyDataSetChanged();
        } else {
            notifyDataSetInvalidated();
        }


        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
               Log.d("-----------", "performFiltering");
        FilterResults results = new FilterResults(); 
        List<String> FilteredArrList = new ArrayList<String>();

        if (objects == null) {
           objects = new ArrayList<String>(autolistArray); // saves

        }


            Locale locale = Locale.getDefault();

            constraint = (String) constraint
                    .toString().toLowerCase(locale);
            if (constraint == null || constraint.length() == 0) {

                // set the Original result to return
            results.count = objects.size();
            results.values = objects;

            } else {
           for (int i = 0; i < objects.size(); i++) {
                String name= objects.get(i);


                if (name.toLowerCase(locale).contains(constraint)
            ) {

                        FilteredArrList.add(model);


                }
            }
            // set the Filtered result to return
            results.count = FilteredArrList.size();

            results.values = FilteredArrList;

            }
            return results;
        }

        @Override
        public CharSequence convertResultToString(Object resultValue) {
            // TODO Auto-generated method stub
            //convert object to string
            Log.d("-----------", "convertResultToString");
            return "";
        }
    };
    return myFilter;
}

}



来源:https://stackoverflow.com/questions/26540448/why-autosuggest-not-display-and-how-to-add-filter-functionalty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!