Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8450625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:01:10+00:00 2026-06-10T11:01:10+00:00

I have a BaseAdapter and want use filter it. Its filled with object (1).

  • 0

I have a BaseAdapter and want use filter it. Its filled with object (1). I want filter using the field “nome”. Its give me error when i try use the filter. check the code:

Object Contato

public class Contato {
    public static final String CONTATO_NOME = "contato_nome";
    public static final String CONTATO_ORG = "contato_org";
    public static final String CONTATO_ID = "contato_id";

    public static String[] colunas = {"_id", Contato.CONTATO_NOME, Contato.CONTATO_ORG, Contato.CONTATO_ID};

    public long _id;
    public String nome;
    public String org;
    public String id;

    public Contato () {
    }

    public Contato (String nome, String org, String id) {
        this.nome = nome;
        this.org = org;
        this.id = id;
    }

    public Contato (long _id, String nome, String org, String id) {
        this.id = id;
        this.nome = nome;
        this.org = org;
        this.id = id;
    }

    public static String[] getColunas() {
        return colunas;
    }
    public static void setColunas(String[] colunas) {
        Contato.colunas = colunas;
    }

    public long get_Id() {
        return _id;
    }
    public void set_Id(long _id) {
        this._id = _id;
    }

    public String getNome() {
        return nome;
    }
    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getOrg() {
        return org;
    }
    public void setOrg(String org) {
        this.org = org;
    }

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
}

baseadapter

public class ContatoListAdapter extends BaseAdapter implements Filterable {
    private Context context;
    private List<Contato> lista;
    private TextView nome;
    private TextView org;
    private LayoutInflater inflater;

    public ContatoListAdapter(Context contexto, List<Contato> lista) {
        this.context = contexto;
        this.lista = lista;
    }

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

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

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        // Recupera a atividade da posição atual
        Contato c = lista.get(position);

        if (position == 0) {

            inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.contato_linha_lista, null);
            // Atualiza o valor do TextView
            nome = (TextView) view.findViewById(R.id.txtContatoListaNome);
            nome.setText(c.nome);

            org = (TextView) view.findViewById(R.id.txtContatoListaOrganizacao);
            org.setText(c.org);

            return view;

        } else {

            inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.contato_linha_lista, null);

            // Atualiza o valor do TextView
            nome = (TextView) view.findViewById(R.id.txtContatoListaNome);
            nome.setText(c.nome);

            org = (TextView) view.findViewById(R.id.txtContatoListaOrganizacao);
            org.setText(c.org);

            return view;

        }
    }

    public Filter getFilter() {
        return new Filter() {

            @SuppressWarnings("unchecked")
            @Override
            protected void publishResults(CharSequence constraint,
                    FilterResults results) {
                lista = (List<Contato>) results.values;
                ContatoListAdapter.this.notifyDataSetChanged();
            }

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {

                List<Contato> filteredResults = getFilteredResults(constraint);

                FilterResults results = new FilterResults();

                results.values = filteredResults;

                return results;
            }
        };
    }

    protected List<Contato> getFilteredResults(CharSequence constraint) {
        // TODO Auto-generated method stub
        return null;
    }

filter’s call

Util.etxtTelaContatosListaBuscar
                .addTextChangedListener(new TextWatcher() {
                    public void afterTextChanged(Editable s) {
                    }

                    public void beforeTextChanged(CharSequence s, int start,
                            int count, int after) {
                    }

                    public void onTextChanged(CharSequence s, int start,
                            int before, int count) {
                        if (Util.adapter != null)
                            Util.adapter.getFilter().filter(s);
                    }
                });

EDIT:

errors I got after type something in search field:

Shutting down VM

thread exiting with uncaught exception (group=0x4001d7d0)

FATAL EXCEPTION: main

java.lang.NullPointerException

at
com.datalabrasil.dtldiscover.ContatoListAdapter.getCount(ContatoListAdapter.java:28)

at
android.widget.AdapterView$AdapterDataSetObserver.onChanged(AdapterView.java:778)

at
android.database.DataSetObservable.notifyChanged(DataSetObservable.java:31)

at
android.widget.BaseAdapter.notifyDataSetChanged(BaseAdapter.java:50)

at
com.datalabrasil.dtldiscover.ContatoListAdapter$1.publishResults(ContatoListAdapter.java:85)

at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:282)

at android.os.Handler.dispatchMessage(Handler.java:99)

at android.os.Looper.loop(Looper.java:123)

at android.app.ActivityThread.main(ActivityThread.java:4627)

at java.lang.reflect.Method.invokeNative(Native Method)

at java.lang.reflect.Method.invoke(Method.java:521)

at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)

at dalvik.system.NativeStart.main(Native Method)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:01:12+00:00Added an answer on June 10, 2026 at 11:01 am
    protected List<Contato> getFilteredResults(CharSequence constraint) {
            // TODO Auto-generated method stub
            return null;
        }
    

    i think you have to fill this method’s body, returning proper result 🙂

    edit: try this:

    protected List<Contato> getFilteredResults(CharSequence constraint) {
        ArrayList<Contacto> filteredList = new ArrayList<Contato>(); 
        for (Contato c:lista) { 
            if (c.nome.trim().toLowerCase().startsWith(constraint.toString().trim().toLowerCase())) { // you can also use contains instead of startsWith...whatever you need 
                filteredList.add(c); 
            } 
        } 
        return filteredList;
        }
    

    edit #2: in the for cycle would be maybe better to use something like getAllContato() or something like that, instead of lista, because after filtering, filtered results rewrite variable lista and that means that with every filtering, you will have less and less items in your list…in that case you are filtering filtered items and so on…i hope you get what i am trying to say. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

HI Guyz i have created a sectioned custom listview using baseadapter its working fine
Im using a BaseAdapter and passing to it a List of objects that have
I want get data in db to listview. but it have error I cannot
I have a search field that I want to autocomplete with data that I
I have a custom baseadapter that i use to populate a listview. each row
I have created a BaseAdapter for a ListView. I want to order the elements
i want to display images from mysql server(testing in localhost) using imageurl,i have images
I have images that I want them to be in a gallery using an
i have a list view with a BaseAdapter for custom view.. i have problem
I have a listview, baseadapter and data from SQLite, including a BLOB. What if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.