litepal插入数据造成的数据重复,已经使用小结

项目账户管理这一块需要搜索,但是是账户列表没有写接口,所以,本地存储,因为之前用的litepal存储,所以为了项目的体积不变大,还是使用litepal查询和存储.新建表改litepal.xml这种我就不说了.
1..list存储:

        List.DataBean> data = paoTangList.getData();
        int size = paoTangList.getData().size();
        list.clear();
        DataSupport.deleteAll(AccountSearchEntity.class);
        for (int i=0;i
//            searchEntity.setAccountHistory(data.get(i).getUsername()+data.get(i).getNick_name());
//            searchEntity.save();
            list.add(new AccountSearchEntity(data.get(i).getUsername()+" - "+data.get(i).getNick_name()));
        }
        DataSupport.saveAll(list);

AccountSearchEntity为账户表实体类,注意的是我之前用的是注视掉的代码进行存储,但是只能存两行数据,本应该是10行的,改成现在这样即可.
2.textwater实时监听,模糊搜索设置recycleview适配器

        if (typeExtra == SEARCHTYPE_ACCOUNT){
            initAccountSearchHoistory();
            mEdittext.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (!TextUtils.isEmpty(s)){
                        List accountSearchEntities =
                                DataSupport.where("accountHistory like ?", "%" + s + "%").find(AccountSearchEntity.class);
                        Log.d("lwp","accountsearchentity:"+accountSearchEntities.toString());
                        //  List all = DataSupport.findAll(AccountSearchEntity.class);
                        //必须得初始化
                        accountSearchRecycleViewAdapter = new AccountSearchRecycleViewAdapter(R.layout.item_account_search,accountSearchEntities);
                        mRvHistory.setLayoutManager(new LinearLayoutManager(SearchActivity.this));
                        accountSearchRecycleViewAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                            @Override
                            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                startActivity(new Intent(SearchActivity.this,
                                        AccounttDetailActivity.class).
                                        putExtra("peoplename",accountSearchRecycleViewAdapter.
                                                getItem(position).getAccountHistory()));
                            }
                        });
                        mRvHistory.setAdapter(accountSearchRecycleViewAdapter);
                    }
                    else {
                        accountSearchRecycleViewAdapter.setNewData(null);
                    }

                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });

ps:新版本的androidstudio没找到ddms,数据库的话直接可以找到界面有下角的DeviceFile Explorer找到data/data/对应包名,将数据库导出来查看即可

你可能感兴趣的:(Android,litepal数据库)