Skip to content Skip to sidebar Skip to footer

[android]how To Do A Search On The First Name And Last Name Only?

This is my main activity .java package com.example.search; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.Iterator; import java.

Solution 1:

try this...

publicUsergetUserByName(String firstName, String lastName) {
    synchronized (userList) {
        Iterator<User> iterator = userList.iterator();
        while (iterator.hasNext()) {
            User user = (User) iterator.next();
            if (user.getFirstName().equals(firstName)
                    && user.getLastName().equals(lastName)) {
                return user;
            }
        }
        // no user foundreturnnull;
    }
}


publicclassMainActivityextendsFragmentActivity {
    privateEditText firstNameEditText;
    privateEditText lastNameEditText;
    privateUserListAdapter<User> adapter;

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        firstNameEditText = (EditText) findViewById(R.id.firstNameEditText);
        lastNameEditText = (EditText) findViewById(R.id.lastNameEditText);
        final ListView listView = (ListView) findViewById(R.id.listView);
        ArrayList<User> usersList = newArrayList<User>();
        // add items to list
        adapter = newUserListAdapter<User>(this,
                R.layout.textview_layout, usersList);
        listView.setAdapter(adapter);
        firstNameEditText.setOnFocusChangeListener(newView.OnFocusChangeListener() {

            @OverridepublicvoidonFocusChange(View v, boolean hasFocus) {
                if(hasFocus) {
                    adapter.setFilterMode(UserListAdapter.MODE_FILTER_BY_FIRST_NAME);
                }
            }
        });
        lastNameEditText.setOnFocusChangeListener(newView.OnFocusChangeListener() {

            @OverridepublicvoidonFocusChange(View v, boolean hasFocus) {
                if(hasFocus) {
                    adapter.setFilterMode(UserListAdapter.MODE_FILTER_BY_LAST_NAME);
                }
            }
        });
        TextWatcher textWatcher = newTextWatcher() {

            @OverridepublicvoidonTextChanged(CharSequence s, int start, int before, int count) {

            }

            @OverridepublicvoidbeforeTextChanged(CharSequence s, int start, int count,
                    int after) {

            }

            @OverridepublicvoidafterTextChanged(Editable s) {
                adapter.getFilter().filter(s.toString());
            }
        };

        firstNameEditText.addTextChangedListener(textWatcher);
        lastNameEditText.addTextChangedListener(textWatcher);
    }

    publicstaticclassUserListAdapter<T> extendsArrayAdapter<T> {
        publicstatic final int MODE_FILTER_BY_FIRST_NAME = 0xFEED;
        publicstatic final int MODE_FILTER_BY_LAST_NAME = 0xDEAF;
        privateList<T> objects;
        private int filterMode;

        publicUserListAdapter(Context context, int textViewResourceId,
                List<T> objects) {
            super(context, textViewResourceId, objects);
            init(objects);
        }

        publicUserListAdapter(Context context, int resource,
                int textViewResourceId, T[] objects) {
            super(context, resource, textViewResourceId, objects);
            init(Arrays.asList(objects));
        }

        privatevoidinit(List<T> objects) {
            this.objects = objects;
        }

        publicUserListAdapter(Context context, int resource,
                int textViewResourceId, List<T> objects) {
            super(context, resource, textViewResourceId, objects);
            init(objects);
        }

        publicUserListAdapter(Context context, int resource,
                int textViewResourceId) {
            super(context, resource, textViewResourceId);
        }

        publicUserListAdapter(Context context, int resource) {
            super(context, resource);
        }

        @OverridepublicFiltergetFilter() {
            returnnewFilter() {

                @OverrideprotectedvoidpublishResults(CharSequence constraint,
                        FilterResults results) {
                    UserListAdapter.this.clear();
                    ArrayList<T> list = (ArrayList<T>) results.values;
                    UserListAdapter.this.addAll(list);
                    if (results.count > 0) {
                        notifyDataSetChanged();
                    } else {
                        notifyDataSetInvalidated();
                    }
                }

                @Overrideprotected synchronized FilterResultsperformFiltering(
                        CharSequence constraint) {
                    FilterResults results = newFilterResults();
                    ArrayList<T> list = newArrayList<T>();
                    Iterator<T> iterator = objects.iterator();
                    while (iterator.hasNext()) {
                        T t = (T) iterator.next();
                        User user = (User) t;
                        String s = null;
                        if (filterMode == MODE_FILTER_BY_FIRST_NAME) {
                            s = user.getFirstName();
                        } elseif (filterMode == MODE_FILTER_BY_LAST_NAME) {
                            s = user.getLastName();
                        }
                        if (s.equals(constraint.toString())) {
                            list.add(t);
                        }
                    }
                    results.values = list;
                    results.count = list.size();
                    return results;
                }
            };
        }

        public int getFilterMode() {
            return filterMode;
        }

        publicvoidsetFilterMode(int filterMode) {
            this.filterMode = filterMode;
        }
    }

    publicstaticclassUser {
        // attributesprivateString accountID;
        privateString companyName;
        privateString designation;
        privateString firstName;
        privateString lastName;
        privateString profileimageBLOB;

        // behaviorspublicUser() // default constructor
        {
            // attributes will be initialized to their default values
        }

        publicUser(String accID, String comName, String design, String fName,
                String lName, String image) // parameterized constructor
        {
            accountID = accID;
            companyName = comName;
            designation = design;
            firstName = fName;
            lastName = lName;
            profileimageBLOB = image;
        }

        // set and get methodspublicvoidsetAccountID(String accID) {
            accountID = accID;
        }

        publicStringgetAccountID() {
            return accountID;
        }

        publicvoidsetCompanyName(String comName) {
            companyName = comName;
        }

        publicStringgetCompanyName() {
            return companyName;
        }

        publicvoidsetDesignation(String design) {
            designation = design;
        }

        publicStringgetDesignation() {
            return designation;
        }

        publicvoidsetFirstName(String fName) {
            firstName = fName;
        }

        publicStringgetFirstName() {
            return firstName;
        }

        publicvoidsetLastName(String lName) {
            lastName = lName;
        }

        publicStringgetLastName() {
            return lastName;
        }

        publicvoidsetProfileImage(String image) {
            profileimageBLOB = image;
        }

        publicStringgetProfileImage() {
            return firstName;
        }

        // toString() methodpublicStringtoString() {
            return accountID + "\t" + companyName + "\t" + designation + "\t\t"
                    + firstName + "\t" + lastName + "\t" + profileimageBLOB;
        }
    }
}

take care about synchronization...

Solution 2:

you need do custom search then you need to add custom ListView search function.

This is big example , what i give you this link. I think it help you.

Link

Thanks

Solution 3:

you need to override getFilter in your adapter class Inside the perforFiltering method use some iterating method to get proper list of data.

Please follow below link

Post a Comment for "[android]how To Do A Search On The First Name And Last Name Only?"