按姓名搜索联系人列表

时间:2019-07-14 10:38:13

标签: java android

我已经成功地设法在arrayList中检索用户的电话联系人并将其上传到我的应用程序,在这里我也能够区分已安装应用程序(用户)和未安装应用程序的联系人然而。我一直在尝试实现一种搜索功能,在该功能中,用户可以按姓名搜索联系人,但没有得到任何结果。

任何人都可以提出建议吗?

此外,我想对联系人的arrayList进行排序,以便已安装应用程序的人按字母顺序显示在recyclerview的顶部,而没有安装应用程序的人则出现在最后一个应用程序用户之后按字母顺序?到目前为止,通过使用Collections.sort()可以按字母顺序排列整个联系人列表。 到目前为止,这是我尝试过的。

ArrayList<UserObject> userList, contactList;
List<UserObject> filteredList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_contacts);

    //adding the filteredList

    contactList = new ArrayList<>();
    userList = new ArrayList<>();
    filteredList = new ArrayList<>();

    mUserList = (RecyclerView) findViewById(R.id.userList);  //recyclerview
    mUserList.setNestedScrollingEnabled(false);
    mUserList.smoothScrollToPosition(0);
    mUserList.setHasFixedSize(true);
    mUserListLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayout.VERTICAL, false);
    mUserList.setLayoutManager(mUserListLayoutManager);
    mUserListAdapter = new UserListAdapter(userList, filteredList);
    mUserListAdapter.setHasStableIds(true);
    mUserList.setAdapter(mUserListAdapter);

    search = findViewById(R.id.searchh); //search is an Edittext
    search.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            filteredList.clear();
            String name = search.getText().toString();
            for (UserObject userObject : userList){
                if (userObject.getName().contains(name)){
                    filteredList.add(userObject);
                }
            }
            mUserListAdapter.notifyDataSetChanged();
        }
        @Override
        public void afterTextChanged(Editable editable) {
        }
    });
    ...
}

//我的UserListAdapter

public class UserListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    ArrayList<UserObject> userList;
    List<UserObject> filteredList; //the filteredList
    private Context context;
    private static final int TYPE_ONE = 1;
    private static final int TYPE_TWO = 2;
    public UserListAdapter(ArrayList<UserObject> userList, List<UserObject> filteredList){
        this.userList = userList;
        this.filteredList = filteredList;
    }

    //some more code

    //BindViewHolder
    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position) {
        switch (holder.getItemViewType()){
            case TYPE_ONE:
                initLayoutOne((ViewHolderOne) holder, position);
                break;

            case TYPE_TWO:
                initLayoutTwo((ViewHolderTwo) holder, position);
                break;

                default:
                    break;
        }
    }

    //case one
    private void initLayoutOne(final ViewHolderOne holder, final int position) {
        holder.mName.setText(userList.get(position).getName());
        holder.mPhone.setText(userList.get(position).getPhone());

        //more code

    });
    ...
}

让我知道是否应该添加任何其他信息,以帮助所有人更好地理解问题。 预先感谢。

1 个答案:

答案 0 :(得分:0)

首先,您需要创建原始列表的副本。仅仅是因为您不想在添加和删除约束时丢失对象:

List<UserObject> filteredList = new ArrayList();

,然后在RecyclerView(或ListView)中显示此列表:

adapter.setItems(filteredList)

然后在textWatcher中,您将过滤原始列表并将相应的项目添加到filteredList中,然后调用adapter.notifyDataSetChanged():

@Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
           String name = search.getText().toString();
           filteredList.clear();
           for (UserObject userObject : userList){
               if (userObject.getName().contains(name)){
                   filteredList.add(userObject);
               }
           }
           adapter.notifyDataSetChanged(); //Do not forget this line
        }

---------编辑---------

1-从适配器中删除过滤的列表。没必要。

2-在您的活动中,按如下所示初始化适配器:

mUserListAdapter = new UserListAdapter(filteredList);