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 8648191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:15:27+00:00 2026-06-12T13:15:27+00:00

I’m making a ListView with data I get from a database placed in a

  • 0

I’m making a ListView with data I get from a database placed in a server. The ListView has TWO TextViews for each row, one for the cities, and the other one for the ID’s each city has on the database, but this textview(the ID’s one) is HIDDEN from the ListView, so the user can only see cities.

Everything works fine, but now I want to add a search bar to the listview so I can filter the cities, but i’m getting the following problems:

1.-The adapter of the ListView is a Simple Adapter, as you can see on the code, and I have found no examples of how to filter a Listview using such an adapter.

2.-I’m getting the info for the ListView through an Async Task, and setting up the ListView inside the onPostExecute method of the Async Task, and again, I have found no examples of filtering it when its in such a place.

Here is the “relevant” code. If somebody needs the complete code, just ask me for it.

public class SQLWebVer extends ListActivity implements OnItemClickListener{
ProgressDialog barraProgreso;
    ArrayList<HashMap<String, String>> listaCiudades;
    .
    .//some more varibles for the class
    .
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sqlwebver);

    // Hashmap de la ListView
    listaCiudades = new ArrayList<HashMap<String, String>>();

    new cargaCiudades().execute();//this is the call to the async task

    }
    class cargaCiudades extends AsyncTask<String, String, String>{//the async task
    .
    .//here are the onPreExecute and doInBackground methods
    .
         protected void onPostExecute(String file_url) {
        barraProgreso.dismiss();//quita la barra de la pantalla
        runOnUiThread(new Runnable(){//actualiza la UI(la listView)

            public void run() {
                // TODO Auto-generated method stub
                ListAdapter adapter = new SimpleAdapter 
                                      (SQLWebVer.this, listaCiudades, 
                                      R.layout.sqlweblist_item,
                          new String[]{"key_id", "key_ciudad"},new int[]
                                      {R.id.ID_CIUDAD, R.id.CIUDAD});

                setListAdapter(adapter);
            }

        });
        }
     }

I have tried to add the filter feature both on the onCreate and onPostExecute method, but with no luck so, any suggestions?

P.D:BONUS question, how could I sort my ListView alphabetically by cities but keeping each ID related to his city?(without changing the php files)

EDIT: I solved the problem by creating a Custom Base Adapter which implements filterable. Wasn´t as hard as I thought at first 😉

  • 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-12T13:15:28+00:00Added an answer on June 12, 2026 at 1:15 pm

    Some time ago I solved this problem but I forget to post the answer that worked for me, so here it is:

    package android.codigo;
    
    import java.util.ArrayList;
    
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.Filter;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    
    public class MiAdaptador extends ArrayAdapter<Item2> {
    
        private ArrayList<Item2> allItems;
        @SuppressWarnings("unused")
        private ArrayList<Item2> subItems;
    
        public ArrayList<Item2> getItems() {
            return allItems;
        }
    
        public MiAdaptador(Context context, int textViewResourceId,
                ArrayList<Item2> items) {
            super(context, textViewResourceId, items);
            this.allItems = items;
            this.subItems = items;
        }
    
        @Override
         public int getCount(){
               return subItems.size();
         }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater) getContext()
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.item2_lista, null);
            }
    
            Item2 it = subItems.get(position);
    
            if (it != null) {
                TextView nombre = (TextView) v.findViewById(R.id.textView1);
                ImageView img = (ImageView) v.findViewById(R.id.imageView1);
                ImageView img2 = (ImageView) v.findViewById(R.id.imageView2);
                ImageView img3 = (ImageView) v.findViewById(R.id.imageView3);
                if (nombre != null) {
                    nombre.setText(it.getNombre());
                }
                if (img != null) {
                    img.setImageResource(it.getID());
                }
                if (img != null) {
                    img2.setImageResource(it.getID2());
                }
                if (img != null) {
                    img3.setImageResource(it.getID3());
                }
            }
    
            return v;
    
        }
        public Filter getFilter() {
            Filter filter = new Filter() {
    
                @Override
                protected FilterResults performFiltering(CharSequence prefijo) {
    
                    FilterResults results = new FilterResults();
                    ArrayList<Item2> aux = new ArrayList<Item2>();
    
                    // El prefijo tiene que ser mayor que 0 y existir
                    if (prefijo != null && prefijo.toString().length() > 0) {
    
                        for (int index = 0; index < allItems.size(); index++) {
                            Item2 si = allItems.get(index);
                            String nombre = si.getNombre();
    
                            String nom[] = nombre.split(" ");
                            for(int i=0; i<nom.length; i++){
                                if (nom[i].toLowerCase().startsWith(prefijo.toString().toLowerCase())) {
                                    aux.add(si);
                                }
                            }
                        }
                        results.values = aux;
                        results.count = aux.size();
                    } else {
                        synchronized (allItems) {
                            results.values = allItems;
                            results.count = allItems.size();
                        }
                    }
                    return results;
                }
    
                @SuppressWarnings("unchecked")
                @Override
                protected void publishResults(CharSequence prefijo,FilterResults results) {
                    subItems = (ArrayList<Item2>) results.values;
                    notifyDataSetChanged();
    
                }
            };
            return filter;
        }
    }
    

    and added this in the main class:

    ListView lv = (ListView)findViewById(R.id.list);
        lv.setTextFilterEnabled(true);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(this);
    
    
    
    
        TextWatcher filtro = new TextWatcher(){
    
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub
    
            }
    
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {
                // TODO Auto-generated method stub
    
            }
    
            public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {
                // TODO Auto-generated method stub
                adapter.getFilter().filter(e.getText().toString());//filtra el texto metido en el edittext en tiempo real
            }
    
        };
    
        e.addTextChangedListener(filtro);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a view passing on information from a database: def serve_article(request, id): served_article
I'm making a simple page using Google Maps API 3. My first. One marker
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

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.