[Android]如何仅对名字和姓氏进行搜索?

时间:2013-12-19 09:59:54

标签: java android

这是我的主要活动.java

  package com.example.search;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Filter;
import android.widget.ListView;

public User getUserByName(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 found
        return null;
    }
}


public class MainActivity extends FragmentActivity {
    private EditText firstNameEditText;
    private EditText lastNameEditText;
    private UserListAdapter<User> adapter;

    @Override
    protected void onCreate(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 = new ArrayList<User>();
        // add items to list
        adapter = new UserListAdapter<User>(this,
                R.layout.textview_layout, usersList);
        listView.setAdapter(adapter);
        firstNameEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus) {
                    adapter.setFilterMode(UserListAdapter.MODE_FILTER_BY_FIRST_NAME);
                }
            }
        });
        lastNameEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus) {
                    adapter.setFilterMode(UserListAdapter.MODE_FILTER_BY_LAST_NAME);
                }
            }
        });
        TextWatcher textWatcher = new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                adapter.getFilter().filter(s.toString());
            }
        };

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

    public static class UserListAdapter<T> extends ArrayAdapter<T> {
        public static final int MODE_FILTER_BY_FIRST_NAME = 0xFEED;
        public static final int MODE_FILTER_BY_LAST_NAME = 0xDEAF;
        private List<T> objects;
        private int filterMode;

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

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

        private void init(List<T> objects) {
            this.objects = objects;
        }

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

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

        public UserListAdapter(Context context, int resource) {
            super(context, resource);
        }

        @Override
        public Filter getFilter() {
            return new Filter() {

                @Override
                protected void publishResults(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();
                    }
                }

                @Override
                protected synchronized FilterResults performFiltering(
                        CharSequence constraint) {
                    FilterResults results = new FilterResults();
                    ArrayList<T> list = new ArrayList<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();
                        } else if (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;
        }

        public void setFilterMode(int filterMode) {
            this.filterMode = filterMode;
        }
    }

    public static class User {
        // attributes
        private String accountID;
        private String companyName;
        private String designation;
        private String firstName;
        private String lastName;
        private String profileimageBLOB;

        // behaviors
        public User() // default constructor
        {
            // attributes will be initialized to their default values
        }

        public User(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 methods
        public void setAccountID(String accID) {
            accountID = accID;
        }

        public String getAccountID() {
            return accountID;
        }

        public void setCompanyName(String comName) {
            companyName = comName;
        }

        public String getCompanyName() {
            return companyName;
        }

        public void setDesignation(String design) {
            designation = design;
        }

        public String getDesignation() {
            return designation;
        }

        public void setFirstName(String fName) {
            firstName = fName;
        }

        public String getFirstName() {
            return firstName;
        }

        public void setLastName(String lName) {
            lastName = lName;
        }

        public String getLastName() {
            return lastName;
        }

        public void setProfileImage(String image) {
            profileimageBLOB = image;
        }

        public String getProfileImage() {
            return firstName;
        }

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

我对@Gopal Rao做了相应的更改,这是我上面的代码,但是我遇到了很多我无法理解的错误,我是否正确地做了?

请告知,它仍然无法正常工作

3 个答案:

答案 0 :(得分:5)

试试这个......

public User getUserByName(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 found
        return null;
    }
}


public class MainActivity extends FragmentActivity {
    private EditText firstNameEditText;
    private EditText lastNameEditText;
    private UserListAdapter<User> adapter;

    @Override
    protected void onCreate(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 = new ArrayList<User>();
        // add items to list
        adapter = new UserListAdapter<User>(this,
                R.layout.textview_layout, usersList);
        listView.setAdapter(adapter);
        firstNameEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus) {
                    adapter.setFilterMode(UserListAdapter.MODE_FILTER_BY_FIRST_NAME);
                }
            }
        });
        lastNameEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus) {
                    adapter.setFilterMode(UserListAdapter.MODE_FILTER_BY_LAST_NAME);
                }
            }
        });
        TextWatcher textWatcher = new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                adapter.getFilter().filter(s.toString());
            }
        };

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

    public static class UserListAdapter<T> extends ArrayAdapter<T> {
        public static final int MODE_FILTER_BY_FIRST_NAME = 0xFEED;
        public static final int MODE_FILTER_BY_LAST_NAME = 0xDEAF;
        private List<T> objects;
        private int filterMode;

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

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

        private void init(List<T> objects) {
            this.objects = objects;
        }

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

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

        public UserListAdapter(Context context, int resource) {
            super(context, resource);
        }

        @Override
        public Filter getFilter() {
            return new Filter() {

                @Override
                protected void publishResults(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();
                    }
                }

                @Override
                protected synchronized FilterResults performFiltering(
                        CharSequence constraint) {
                    FilterResults results = new FilterResults();
                    ArrayList<T> list = new ArrayList<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();
                        } else if (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;
        }

        public void setFilterMode(int filterMode) {
            this.filterMode = filterMode;
        }
    }

    public static class User {
        // attributes
        private String accountID;
        private String companyName;
        private String designation;
        private String firstName;
        private String lastName;
        private String profileimageBLOB;

        // behaviors
        public User() // default constructor
        {
            // attributes will be initialized to their default values
        }

        public User(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 methods
        public void setAccountID(String accID) {
            accountID = accID;
        }

        public String getAccountID() {
            return accountID;
        }

        public void setCompanyName(String comName) {
            companyName = comName;
        }

        public String getCompanyName() {
            return companyName;
        }

        public void setDesignation(String design) {
            designation = design;
        }

        public String getDesignation() {
            return designation;
        }

        public void setFirstName(String fName) {
            firstName = fName;
        }

        public String getFirstName() {
            return firstName;
        }

        public void setLastName(String lName) {
            lastName = lName;
        }

        public String getLastName() {
            return lastName;
        }

        public void setProfileImage(String image) {
            profileimageBLOB = image;
        }

        public String getProfileImage() {
            return firstName;
        }

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

注意同步...

答案 1 :(得分:0)

您需要进行自定义搜索,然后需要添加自定义ListView搜索功能。

这是一个很好的例子,我给你这个链接。我认为它对你有帮助。

  

Link

由于

答案 2 :(得分:0)

您需要在适配器类中覆盖getFilterperforFiltering方法中使用 一些迭代方法来获取正确的数据列表。

请按照以下link

进行操作