在与数据库相关的Listview中搜索并使用翻新

时间:2018-07-31 15:10:25

标签: android

在我的列表视图中,我有一个用户列表,我想根据Noun_Pronoun属性从该列表中搜索一个对象。但是结果总是有错误的对象,删除搜索字符串时,结果为空! ................................................... .............................

public class InvitationActivity extends AppCompatActivity {

    ListView ClassInvitLv;
    SearchView ClassInvitSv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invitation);


        ClassInvitLv=findViewById(R.id.lv);
        ClassInvitSv=findViewById(R.id.sv);
        getUsers();

    }


    private void getUsers(){

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(getString(R.string.BaseUrl))
                .addConverterFactory(GsonConverterFactory.create())
                .build();


        MyInterface myInterface=retrofit.create(MyInterface.class);

        Call<List<User>> call = myInterface.getUsers();

        call.enqueue(new Callback<List<User>>() {
            @Override
            public void onResponse(Call<List<User>> call, Response<List<User>> response) {

                final List<User> UserList = response.body();

                final UsersAdapter usersAdapter=new UsersAdapter(InvitationActivity.this, R.layout.custom_row, UserList );
                ClassInvitLv.setAdapter(usersAdapter);



                ClassInvitSv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                    @Override
                    public boolean onQueryTextSubmit(String s) {
                        return false;
                    }

                    @Override
                    public boolean onQueryTextChange(String s) {

                        for (int c=0; c<UserList.size(); c++) {

                            
 if(!(UserList.get(c).getNoun_Pronoun()).contains(s))

                                UserList.remove(UserList.get(c));

                        }

                        usersAdapter.notifyDataSetChanged();

                      return false;
                    }
                });

            }


            @Override
            public void onFailure(Call<List<User>> call, Throwable t) {
                Toasty.error(getApplicationContext(),"failure",Toast.LENGTH_SHORT).show();
            }
        });


    }
}

如何解决此问题?

1 个答案:

答案 0 :(得分:0)

我在这里修改了您的代码,它应该可以解决您的问题。期望在ViewModel类中,您不会复制构造函数中传递的列表的任何副本。如果这样做,则在UsersAdapter类中创建一个方法,说UsersAdapter并进行相应的处理。

updateUserList(List<User> filterList)

最好在public void onResponse(Call<List<User>> call, Response<List<User>> response) { final List<User> UserList = response.body(); final List<User> filterList = new ArrayList<User>(); filterList.addAll(UserList); final UsersAdapter usersAdapter=new UsersAdapter(InvitationActivity.this, R.layout.custom_row, filterList ); ClassInvitLv.setAdapter(usersAdapter); ClassInvitSv.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String s) { return false; } @Override public boolean onQueryTextChange(String s) { filterList.clear(); for (int c=0; c<UserList.size(); c++) { if((UserList.get(c).getNoun_Pronoun()).contains(s)) filterList.add(UserList.get(c)); } usersAdapter.notifyDataSetChanged(); return false; } }); } 类中创建一个如下所示的方法,并以UserAdapter的方式从方法onQueryTextChange进行调用;在哪里调用usersAdapter.updateUserList(filterList);

usersAdapter.notifyDataSetChanged()