我试图在SQL Lite数据库中使用SearchView进行过滤,然后在ListView中显示。我正在使用2 List<对象>帮助我做到这一点。首先,我使用bd.buscar()从SQL Lite中检索数据并传递给我的List< Produto> listp。然后我分配给List< Produto> Listaux从listp检索的值。 ListView显示数据库中的所有数据。然后我使用SearchView方法onQueryTextChange来做过滤器,ListView变空。 我使用Log来跟踪listp的大小,但是当filterProduto方法启动时,listp变为空。
public class ActivityListaProduto extends Activity {
//ListActivity
protected static final String MsnLog = "log-MeuSQLite";
private ListView my_List;
private ActivityListaProdutoAdapter Adapter;
private SearchView searchView;
private Crud crud;
private Button deletarBt;
List<Produto> listp;
List<Produto> Listaux;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(MsnLog, " ");
Log.i(MsnLog, "========== ActivityListaProduto --- onCreate ============= ");
setContentView(R.layout.activity_lista_produto);
//relate the listView from java to the one created in xml
my_List = (ListView) findViewById(R.id.mylist);
//prepare the SearchView
searchView = new SearchView(this);
searchView = (SearchView) findViewById(R.id.search);
caregardb();
Adapter = new ActivityListaProdutoAdapter(this, Listaux);
my_List.setAdapter(Adapter);
// Search
searchView.setOnQueryTextListener(new Searchable());
deletarBt = (Button) findViewById(R.id.btnBusca);
deletarBt.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
Log.i(MsnLog, " ");
Log.i(MsnLog, "---------- ActivityListaProduto --- onClick ----------");
// Search
Searchable b;
searchView.setOnQueryTextListener(new Searchable());
Adapter.notifyDataSetChanged();
/*
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView;
searchView = (SearchView) findViewById(R.id.search);
Log.i(MsnLog, "searchView =" + searchView);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
Log.i(MsnLog, "searchView =" + searchView.getClass().toString());
searchView.setQueryHint(getResources().getString(R.string.search_hint));
Log.i(MsnLog, " ---- > vai para a tela SearchableActivity ");
//ActivityListaProdutoAdapter.this.notifyDataSetChanged();
//Adapter.notifyDataSetChanged();
//Log.i(MsnLog, " ---- > Adapter.notifyDataSetChanged()");
*/
}
});
}
public void caregardb(){
Crud bd = new Crud(this);
listp = bd.buscar();
Log.i(MsnLog, "onCreate -- listp.size()= "+ listp.size());
Listaux = listp;
Log.i(MsnLog, "onCreate -- Listaux.size() 2= "+ Listaux.size());
Log.i(MsnLog, "onCreate -- listp.size()= "+ listp.size());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_activity_lista_produto, menu);
// test
/*
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
*/
// Search
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView;
MenuItem item = menu.findItem(R.id.menu_search);
if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ){
searchView = (SearchView) item.getActionView();
}
else{
searchView = (SearchView) MenuItemCompat.getActionView(item);
}
searchView.setSearchableInfo( searchManager.getSearchableInfo( getComponentName() ) );
searchView.setQueryHint( getResources().getString(R.string.search_hint) );
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case R.id.menu_search:
onSearchRequested();
return true;
default:
return false;
}
/*
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
*/
//return super.onOptionsItemSelected(item);
}
public class Searchable implements SearchView.OnQueryTextListener {
@Override
public boolean onQueryTextSubmit(String query) {
Log.i(MsnLog, "---------- Searchable --- onQueryTextSubmit ----------");
Log.i(MsnLog, "onQueryTextSubmit(String" + query + ") ");
filterProduto(query);
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
Log.i(MsnLog, "---------- Searchable --- onQueryTextChange ----------");
Log.i(MsnLog, "onQueryTextChange(String " + newText + " ) ");
filterProduto(newText);
return false;
}
public void filterProduto( String q ){
Log.i(MsnLog, " ");
Log.i(MsnLog, "---------- Searchable --- filterProduto ----------");
Log.i(MsnLog, " String q = "+ q);
Listaux.clear();
Log.i(MsnLog, "filterProduto -- listp.size()= "+ listp.size());
for( int i = 0, tamI = listp.size(); i < tamI; i++ ){
Log.i(MsnLog, " int i = 0,"+ tamI +" = "+listp.size()+ "; "+ i + " < " +tamI+ "; "+i+"++ ");
if( listp.get(i).getNmProduto().toLowerCase().startsWith( q.toLowerCase() ) ){
Log.i(MsnLog, "listp.get(i) = "+ listp.get(i));
Listaux.add( listp.get(i) );
Log.i(MsnLog, "Listaux.add) = "+ listp.get(i));
}
}
if( Listaux.isEmpty() ){
Log.i(MsnLog, "Lista - Listaux Vasia");
}
Log.i(MsnLog, "Listaux " + Listaux.getClass());
Adapter.notifyDataSetChanged();
}
}
}