删除数据库内容后searchView不起作用

时间:2018-10-15 06:47:26

标签: android searchview

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

MainActivity.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);
 }
}

BeneficiaryRecyclerAdapter内部

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

起初,searchView可以工作很多次,但是删除某些行后,它无响应。我已经使用Log.d进行了检查,问题是onQueryTextChange(String newText)没有得到任何值,但是在mainActivity恢复后可以工作。因此,请让我发现错误。

1 个答案:

答案 0 :(得分:0)

尝试添加

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

clearActionMode()