Search View in a Listview

Asked

Viewed 1,055 times

0

I am trying to implement a Search View in a List View of my project. I saw some tutorials but I could not implement any because there was always an error here and there by the peculiarities of each list and code. So I come to the best ones to ask if someone can help me. I ask them to be the most specific because I don’t have much experience. Thank you.

 ........

public class ListClientes extends AppCompatActivity implements AdapterView.OnItemLongClickListener, AdapterView.OnItemClickListener {
ListView lista;
ArrayList<Cliente> clientes;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_clientes);

    lista = (ListView) findViewById(R.id.listview);

    lista.setOnItemLongClickListener(this);
    lista.setOnItemClickListener(this);

    atualizar(null);

}


public void atualizar(View view) {
    ClienteDao cliDao = new ClienteDao();

    clientes = cliDao.getListagem();
    lista.setAdapter(new ClienteAdapter(getBaseContext(), clientes));

}

public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
    Intent cadClienteIntent = new Intent(this, CadCliente.class);
    cadClienteIntent.putExtra("Cliente", clientes.get(position));
    startActivity(cadClienteIntent);
    return true;
}

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Intent cadPedidoIntent = new Intent(this, CadPedido.class);
    cadPedidoIntent.putExtra("Cliente", clientes.get(position));
    startActivity(cadPedidoIntent);


}


}

My client Adapter

 ....... 

 public class ClienteAdapter extends BaseAdapter {

private Context context;
private List<Cliente> clientes;

public ClienteAdapter(Context context, List<Cliente> clientes) {
    this.context = context;
    this.clientes = clientes;

}

public int getCount() {
    return clientes.size();
}

public Object getItem(int position) {
    return clientes.get(position);
}

public long getItemId(int position) {
    return clientes.get(position).getId();
}



public View getView(int position, View convertView, ViewGroup parent) {
    Cliente cliente = clientes.get(position);

    LayoutInflater layout = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View view = layout.inflate(R.layout.linhacli, null);

    Log.i("AULA", "Montou:" + cliente.getNome());
    Log.e("ERRO", "Valor da variavel estava nullo!");

    ImageView imgImageView = (ImageView) view.findViewById(R.id.imgCliente);
    imgImageView.setImageBitmap(BitmapFactory.decodeFile(cliente.getPathImagem()));

    TextView edNome = (TextView) view.findViewById(R.id.textView1);
    edNome.setText(cliente.getNome());

    TextView lblTelefone = (TextView) view.findViewById(R.id.textView2);
    lblTelefone.setText(cliente.getTelefone());
    lblTelefone.setText(Mask.addMask(cliente.getTelefone(), "(##)####-####"));

    return view;
}


}
  • Not if you’ve already done it, at least you haven’t commented. Missing base layout file for Listview. for example, you have your activity_main.xml which is the layout where you will have the Listview view and a part Xyz.mlx file that contains the structure of how the data will be presented in the main_activity.xml view. And one more thing, is it necessary to implement another adapter? maybe Arrayadapter would solve your problem in the case described above

  • Yes.. the rest I have already done.. I have the layout of the line "client" in my case, I believe that it is not necessary another adapter..

1 answer

2


Try it like this in the menu folder
item_menu_search.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:id="@+id/search"
    android:actionViewClass="android.widget.SearchView"
    android:icon="@android:drawable/ic_menu_search"
    android:showAsAction="always"
    android:title="@string/search_title"/>

</menu>

In Activity

public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    // Carrega o xml do menu Pesquisa.
    getMenuInflater().inflate(R.menu.item_menu_pesquisa, menu); 
    SearchView sv = (SearchView) menu.findItem(R.id.search).getActionView();
    sv.setOnQueryTextListener(new SearchFiltro());

    return (true);
}

public class SearchFiltro implements OnQueryTextListener {

    @Override
    public boolean onQueryTextSubmit(String query) {
        // TODO Auto-generated method stub
        //Log.i("Script", "onQueryTextSubmit-> " + query);
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {

        if (newText.toString() != null && !newText.toString().equals("")) {
            SeuAdapter adpter = new SeuAdapter(MainActivity.this,
                    new ControleBanco(MainActivity.this).lista_pesquisa(newText.toString()));
            listView.setAdapter(null);
            listView.setAdapter(adpter);
        }

        return false;
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.