删除数据库内容后搜索视图不起作用



我是Android编程的初学者。我从 3 天开始遇到这个问题的麻烦。请提出任何建议。

主要活动.java

public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener,
View.OnLongClickListener, BeneficiaryRecyclerAdapter.ListItemClickListener {
private AppCompatActivity activity = MainActivity.this;
public boolean is_in_action_mode = false;
private RecyclerView recyclerViewBeneficiary;
private Beneficiary beneficiary;
private ArrayList<Beneficiary> listBeneficiary;
ArrayList<Beneficiary> selection_list = new ArrayList<>();
private BeneficiaryRecyclerAdapter beneficiaryRecyclerAdapter;
private DatabaseHelper databaseHelper;
Toolbar toolbar;
TextView counter_text_view;
int counter = 0, id;
private SQLiteDatabase mDb;
private final static String TAG = "check";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

//toolbar.inflateMenu(R.menu.menu_activity_main);
counter_text_view = (TextView) findViewById(R.id.counter_text);
counter_text_view.setText(getString(R.string.app_name));
recyclerViewBeneficiary = (RecyclerView) findViewById(R.id.listRecyclerView);
initObjects();

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.addAccountFab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, 
AccountDetailActivity.class);
startActivity(intent);
}
});
}
private void initObjects() {
listBeneficiary = new ArrayList<>();
beneficiaryRecyclerAdapter = new BeneficiaryRecyclerAdapter(listBeneficiary, MainActivity.this,MainActivity.this);
Log.d(TAG,"listBeneficiary size[initObject()] = "+ listBeneficiary.size());
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerViewBeneficiary.setLayoutManager(mLayoutManager);
recyclerViewBeneficiary.setItemAnimator(new DefaultItemAnimator());
//recyclerViewBeneficiary.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL)); // For Vertical line separation
recyclerViewBeneficiary.setHasFixedSize(true);
recyclerViewBeneficiary.setAdapter(beneficiaryRecyclerAdapter);
databaseHelper = new DatabaseHelper(activity);
getDataFromSQLite();
}
/**
* This method is to fetch all user records from SQLite
*/
private void getDataFromSQLite() {
// AsyncTask is used that SQLite operation not blocks the UI Thread.
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
listBeneficiary.clear();
listBeneficiary.addAll(databaseHelper. getAllBeneficiary());
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
beneficiaryRecyclerAdapter.notifyDataSetChanged();
}
}.execute();
}

@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
newText = newText.toLowerCase();
ArrayList<Beneficiary> newList = new ArrayList<>();
Log.d(TAG,"listBeneficiary size[onQuery] = "+ listBeneficiary.size());
for (Beneficiary beneficiary: listBeneficiary) {
String name = beneficiary.getTitle().toLowerCase();
if (name.contains(newText))
newList.add(beneficiary);
}
beneficiaryRecyclerAdapter.setFilter(newList);
return true;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_activity_main,menu);
MenuItem menuItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) menuItem.getActionView();
searchView.setQueryHint("Search by title");
searchView.setOnQueryTextListener(this);
searchView.setIconified(false);
return true;
}
@Override
public boolean onLongClick(View v) {
toolbar.getMenu().clear();
toolbar.inflateMenu(R.menu.menu_action_mode);
toolbar.setBackgroundColor(getResources().getColor(R.color.colorActionMode));
counter_text_view.setVisibility(View.VISIBLE);
counter_text_view.setText("0 items selected");
is_in_action_mode = true;
beneficiaryRecyclerAdapter.notifyDataSetChanged();     
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
return true;
}
public void clearActionMode(){
is_in_action_mode = false;
toolbar.getMenu().clear();
toolbar.inflateMenu(R.menu.menu_activity_main);
toolbar.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
getSupportActionBar().setDisplayHomeAsUpEnabled(false); // old = true
counter_text_view.setText(getString(R.string.app_name));        // New,good
counter = 0;
selection_list.clear();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId()==R.id.item_delete){
for (Beneficiary contact : selection_list) {       //New
listBeneficiary.remove(contact);        //New
databaseHelper.deleteSelectedItems(contact.getId());
}

beneficiaryRecyclerAdapter.notifyDataSetChanged(); //new
clearActionMode()
}
else if(item.getItemId()== android.R.id.home){
clearActionMode();
beneficiaryRecyclerAdapter.notifyDataSetChanged();
}
return true;
}
Intent intent = new Intent(this, AccountDetailActivity.class);
Uri currentUri = 
ContentUris.withAppendedId(BeneficiaryEntry.CONTENT_URI, id);
intent.setData(currentUri);
startActivity(intent);
}
}

内部受益人回收器适配器

public void setFilter(ArrayList<Beneficiary> newList) {
listBeneficiary = new ArrayList<>();
listBeneficiary.addAll(newList);
notifyDataSetChanged();
}

起初,searchView工作了很多次,但是在我删除某些行后,它没有响应。我已经使用Log.d进行了检查,问题是onQueryTextChange(String newText)没有得到任何值,但在恢复主活动后工作。所以请让我们找到我的错误。

尝试添加

MenuItem menuItem = toolbar.getMenu().findItem(R.id.action_search);
SearchView searchView = (SearchView) menuItem.getActionView();
searchView.setQueryHint("Search by title");
searchView.setOnQueryTextListener(this);

clearActionMode().

相关内容

  • 没有找到相关文章

最新更新