Jump to content

Como implementar um Search numa ListView?


Psycop

Recommended Posts

Boa tarde, estou a tentar implementar um Search numa determinada listView, mas sem sucesso, será que me podem ajudar?

O código que tenho até ao momento é o seguinte:

Activity principal:

public class CorposBombeirosActivity extends AppCompatActivity {

ListView lv;
DbHelper dbHelper;
LVAdapter lvAdapter;
ArrayList<CorpoBombeiros> listCorposBombeiros;

//Variable Declaration
private AdView mAdView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // remove title
    getSupportActionBar().hide();
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    //Bloquear Orientação Ecrã a Portrait
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    setContentView(R.layout.activity_corpos_bombeiros);


    //Publicity
    mAdView = (AdView) findViewById(R.id.adView);
    //mAdView.setAdSize(AdSize.BANNER);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);


    //Método que desencadeia os métodos de cópia da BD na classe DbHelper para a pasta databases no Android
    DbHelper myDbHelper = new DbHelper(this);

    try {

        myDbHelper.createDataBase();

    } catch (IOException ioe) {

        throw new Error("Unable to create database");

    }

    try {

        myDbHelper.openDataBase();

    } catch (SQLException sqle) {

        throw sqle;

    }


    dbHelper = new DbHelper(this);
    lv = (ListView) findViewById(R.id.listView);
    listCorposBombeiros = new ArrayList<CorpoBombeiros>();
    listCorposBombeiros.addAll(dbHelper.getListaCorposBombeiros());

    lvAdapter = new LVAdapter(this, R.layout.corpo__bombeiros_item, listCorposBombeiros);
    lv.setAdapter(lvAdapter);


    //Open Detail Activity
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            Intent intent = new Intent(CorposBombeirosActivity.this, CorposBombeirosDetailActivity.class);

            CorpoBombeiros p = (CorpoBombeiros) lvAdapter.getItem(arg2);
            intent.putExtra("CORPO_BOMBEIROS_ID", p.getID());
            startActivity(intent);
        }
    });

}

Classe LvAdapter:

public class LVAdapter extends BaseAdapter{
Context context;
int layout_id;
private final List<CorpoBombeiros> items;

//Constructor
public LVAdapter(final Context context, final int layout_id, final List<CorpoBombeiros> items){
    this.context = context;
    this.layout_id = layout_id;
    this.items = items;
}

//Count items
public int getCount(){
    return this.items.size();
}

public Object getItem(int arg0){
    return  this.items.get(arg0);
}

public long getItemId(int arg0){
    return 0;
}

@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {

    final CorpoBombeiros row = this.items.get(arg0);

    View itemView = null;

    if (arg1 == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        itemView = inflater.inflate(layout_id, null);
    } else {
        itemView = arg1;
    }

    String cod_cb = Integer.toString(row.getCODIGO_CB());
    TextView textViewCodigo_cb = (TextView)itemView.findViewById(R.id.textView_nome);
    textViewCodigo_cb.setText(cod_cb);

    TextView textViewCb = (TextView)itemView.findViewById(R.id.textView_cb);
    textViewCb.setText(row.getCB());

    TextView textViewConcelho = (TextView)itemView.findViewById(R.id.textView_AOS);
    textViewConcelho.setText(row.getCONCELHO());

    return itemView;
}

Alguém me pode ajudar e explicar como posso inserir aqui uma caixa de pesquisa do tipo search box que vá filtrando os items da listview a medida que vamos escrevendo algo na search box?

Link to comment
Share on other sites

  • 1 month later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.