如何在Android中将字符串与Autocomplete ListView进行比较

时间:2016-01-15 06:05:29

标签: android

我想将字符串与自动完成列表视图进行比较,并从自动完成列表视图中删除该字符串,我不想在自动完成列表视图中显示该字符串。请有人建议我如何从自动完成列表视图中删除可比较的字符串。谢谢你提前。

这是我的AutoCompleteAdapter类代码。

public class AutoCompleteAdapter extends ArrayAdapter<AutoCompleteObject> {

    Context context;
    int resource, textViewResourceId;
    List<AutoCompleteObject> items, tempItems, suggestions;
    String str_Authentication_Token ,str_UserId ,str_UserName ,str_UserRole , strUserId;

    public AutoCompleteAdapter(Context context, int resource, int textViewResourceId, List<AutoCompleteObject> items) {
        super(context, resource, textViewResourceId, items);
        this.context = context;
        this.resource = resource;
        this.textViewResourceId = textViewResourceId;
        this.items = items;
        tempItems = new ArrayList<AutoCompleteObject>(items); // this makes the difference.
        suggestions = new ArrayList<AutoCompleteObject>();

        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
        str_Authentication_Token = sharedPreferences.getString("strAuthentication_Token", "");
        str_UserId = sharedPreferences.getString("strUserId", "");
        str_UserName = sharedPreferences.getString("strUserName", "");
        str_UserRole = sharedPreferences.getString("strUserRole","");

        Log.e(" "," str_Authentication_Token " + str_Authentication_Token + " str_UserId "+ str_UserId + " str_UserName "+ str_UserName + " str_UserRole " + str_UserRole);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.serach_arraylist, parent, false);
        }
        AutoCompleteObject autoCompleteObjects = items.get(position);
        if (autoCompleteObjects != null)
        {
            TextView lblName = (TextView) view.findViewById(R.id.textView_Useranames);
            ImageView imgFolowUp = (ImageView)view.findViewById(R.id.imageView_followUp);
            if (lblName != null)
            {
                lblName.setText(autoCompleteObjects.getInspectorName());
                String strlblName = lblName.getText().toString().trim();
                String strActiveStatus = autoCompleteObjects.getActiveStatus();
                Log.e(" strActiveStatus "," #### "+ strActiveStatus );
                String strFollowUpStatus = autoCompleteObjects.getFollowFlag();

                String strInspectorId = autoCompleteObjects.getInspectorId();
                Log.e(" strInspectorId "," = "+strInspectorId);

                if(str_UserId.equals(strInspectorId))
                {
                    items.remove(str_UserId);
                    notifyDataSetChanged();
                }

                if(strActiveStatus.equals("1") && strFollowUpStatus.equals("1"))
                {
                    Log.e("strFollowUpStatus ", " = " + strFollowUpStatus + " strlblName = " + strlblName + " strActiveStatus " + strActiveStatus +" strFollowUpStatus " + strFollowUpStatus);
                    imgFolowUp.setImageResource(R.drawable.follow_grren_mark);
                }
            }
        }
        return view;
    }

    @Override
    public Filter getFilter() {
        return nameFilter;
    }

    /**
     * Custom Filter implementation for custom suggestions we provide.
     */
    Filter nameFilter = new Filter() {
        @Override
        public CharSequence convertResultToString(Object resultValue) {
            String str = ((AutoCompleteObject) resultValue).getInspectorName();
            return str;
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            if (constraint != null) {
                suggestions.clear();
                for (AutoCompleteObject autoCompleteObject : tempItems) {
                    if (autoCompleteObject.getInspectorName().toLowerCase().contains(constraint.toString().toLowerCase())) {
                        suggestions.add(autoCompleteObject);
                    }
                }
                FilterResults filterResults = new FilterResults();
                filterResults.values = suggestions;
                filterResults.count = suggestions.size();
                return filterResults;
            } else {
                return new FilterResults();
            }
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            List<AutoCompleteObject> filterList = (ArrayList<AutoCompleteObject>) results.values;
            if (results != null && results.count > 0)
            {
                clear();
                for (AutoCompleteObject autoCompleteObject : filterList) {
                    add(autoCompleteObject);
                    notifyDataSetChanged();
                }
            }
        }
    };
}

0 个答案:

没有答案