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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:20:24+00:00 2026-06-16T01:20:24+00:00

I have implmented Search view which filters my List View Items. When i enter

  • 0

I have implmented Search view which filters my List View Items. When i enter any text it filters the List but when i exit the Search view it doesnt return back the Original List Items.

public class PlacesListAdapter extends ArrayAdapter<Place> implements
        Filterable {
    public Context context;
    private List<Place> places, orig, itemDetailsrrayList;
    private PlaceFilter filter;

    public PlacesListAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
    }

    public PlacesListAdapter(Context context, int resource, List<Place> places) {
        super(context, resource, places);
        this.context = context;
        this.places = places;

        itemDetailsrrayList = places;
        orig = itemDetailsrrayList;

        filter = new PlaceFilter();
        // imageLoader = new ImageLoader(context.getApplicationContext());

    }

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

    public Place getItem(int position) {
        return itemDetailsrrayList.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        // View view = convertView;
        // Place p = places.get(position);

        if (convertView == null) {
            LayoutInflater viewInflater;
            viewInflater = LayoutInflater.from(getContext());
            convertView = viewInflater.inflate(R.layout.item_place, null);

            holder = new ViewHolder();
            holder.placeTitle = (TextView) convertView
                    .findViewById(R.id.place_title);
            holder.placeDistance = (TextView) convertView
                    .findViewById(R.id.place_distance);
            holder.placeCategoryIcon = (ImageView) convertView
                    .findViewById(R.id.place_category_icon);

            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.placeTitle.setText(itemDetailsrrayList.get(position)
                .getPlaceTitle());
        holder.placeDistance.setText("200");
        holder.placeCategoryIcon.setImageResource(R.drawable.icon_category);

        return convertView;
    }

    static class ViewHolder {
        TextView placeId;
        TextView placeTitle;
        TextView placeDistance;
        ImageView placeCategoryIcon;
    }

    @Override
    public Filter getFilter() {
        // TODO Auto-generated method stub
        return filter;
    }

    private class PlaceFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults oReturn = new FilterResults();
            ArrayList<Place> results = new ArrayList<Place>();
            if (orig == null)
                orig = itemDetailsrrayList;
            if (constraint != null) {
                if (orig != null && orig.size() > 0) {
                    for (Place g : orig) {
                        if (g.getPlaceTitle().toLowerCase()
                                .contains(constraint.toString().toLowerCase()))
                            results.add(g);
                    }
                }
                oReturn.values = results;
            }
            return oReturn;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint,
                FilterResults results) {
            itemDetailsrrayList = (ArrayList<Place>) results.values;
            notifyDataSetChanged();
        }

    }
}

MainActivity.java

public class MainActivity extends SherlockFragmentActivity implements
        SearchView.OnQueryTextListener {
    protected static CharSequence[] _categories = { "Amusement Park",
            "Bird Sanctuary", "Wild Life", "River", "Hill Station", "Temple",
            "Rafting", "Fishing", "Hiking", "Museums" };

    protected static boolean[] _selections = new boolean[_categories.length];

    private final String[] places = new String[] { "Mysore", "Bangalore",
            "Mangalore", "Wayanad", "Bandipur National Park", "Chickmaglur",
            "Bandipura", "Coorg", "Kodaikanal", "Hampi", "Ghati Subramanya",
            "Mekedatu", "Muththathhi", "Shivasamudram", "Talakadu",
            "Savana Durga" };

    public SearchView mSearchView;
    private TextView mStatusView;

    private Menu mainMenu = null;

    PlacesListAdapter adapter;
    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
        setContentView(R.layout.activity_main);

        Log.i("Nomad", "onCreate");

        List<Place> thePlaces = new ArrayList<Place>();
        for (int i = 0; i < places.length; i++) {
            Place pl = new Place("NO_ID", places[i], "NO_DISTANCE",
                    "NO_CATEGORYICON");
            thePlaces.add(pl);
        }

        listView = (ListView) findViewById(R.id.place_list);
        adapter = new PlacesListAdapter(MainActivity.this, R.layout.item_place,
                thePlaces);

        listView.setAdapter(adapter);
        listView.setTextFilterEnabled(true);

        mSearchView = (SearchView) findViewById(R.id.action_search);

        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> a, View view, int position,
                    long id) {

                Toast.makeText(MainActivity.this, "Test", Toast.LENGTH_SHORT);
                startActivity(new Intent(MainActivity.this, PlaceActivity.class));
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        Log.i("Nomad", "onCreateOptionsMenu");
        super.onCreateOptionsMenu(menu);
        MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.activity_main, menu);

        mainMenu = menu;

        MenuItem searchItem = menu.findItem(R.id.action_search);

        // Search View
        mSearchView = (SearchView) searchItem.getActionView();
        setupSearchView(searchItem);

        mSearchView.setOnSearchClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // hide action item
                if (mainMenu != null) {
                    mainMenu.findItem(R.id.action_category).setVisible(false);
                    mainMenu.findItem(R.id.action_sort).setVisible(false);
                }

            }
        });
        mSearchView.setOnCloseListener(new SearchView.OnCloseListener() {
            @Override
            public boolean onClose() {
                // re-show the action button
                if (mainMenu != null) {
                    mainMenu.findItem(R.id.action_category).setVisible(true);
                    mainMenu.findItem(R.id.action_sort).setVisible(true);
                }
                return false;

            }
        });

        Log.i("Nomad", "after setupSearchView()");
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {

        case R.id.action_search:
            Toast.makeText(this, "Searh", Toast.LENGTH_LONG).show();
            Log.i("Nomad", "Click Search");
            break;

        case R.id.action_category:
            showDialog();
            break;
        case R.id.action_sort_alpha_az:
            Toast.makeText(this, "Alpha AZ", Toast.LENGTH_LONG).show();
            break;
        case R.id.action_sort_alpha_za:
            Toast.makeText(this, "Alpha ZA", Toast.LENGTH_LONG).show();
            break;
        case R.id.action_sort_dist_nf:
            Toast.makeText(this, "Dist NF", Toast.LENGTH_LONG).show();
            break;
        case R.id.action_sort_dist_fn:
            Toast.makeText(this, "Dist FN", Toast.LENGTH_LONG).show();
            break;
        default:
            // return super.onOptionsItemSelected(item);
            break;
        }
        return true;
    }

    private void setupSearchView(MenuItem searchItem) {
        mSearchView.setIconifiedByDefault(true);
        mSearchView.setOnQueryTextListener(this);
        mSearchView.setSubmitButtonEnabled(false);

        searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
        // | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
    }

    public boolean onQueryTextChange(String newText) {
        Log.i("Nomad", "onQueryTextChange");

        if (TextUtils.isEmpty(newText)) {
            Log.i("Nomad", "onQueryTextChange Empty String");
            listView.clearTextFilter();
        } else {
            Log.i("Nomad", "onQueryTextChange " + newText.toString());
            adapter.getFilter().filter(newText.toString());
        }
        return true;
    }

    public boolean onQueryTextSubmit(String query) {
        Log.i("Nomad", "onQueryTextSubmit");
        return false;
    }

    public boolean onClose() {
        Log.i("Nomad", "onClose");
        return false;
    }

    protected boolean isAlwaysExpanded() {
        return false;
    }


}
  • 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-16T01:20:25+00:00Added an answer on June 16, 2026 at 1:20 am

    Your two ArrayList with the adapter’s object are pointing to the same object because of this lines:

    itemDetailsrrayList = places;
    orig = itemDetailsrrayList;
    

    The orig list should be kept intact so you have the original data to return to when needed:

    orig = new ArrayList<String>(itemDetailsrrayList);
    

    If this doesn’t solve the problem you may want to show some code related to the search view.

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

Sidebar

Related Questions

I have implemented a Custom List View which displays text along with image. The
In my website I have implemented search feature using full-text search. It works fine
I have a search box that is implemented as a partial view. I would
I have implemented Full Text Search on one of Sql Server 2008 Table. Query
I have implemented Full Text Search on one of Sql Server 2008 Table. I
My Normal search in the activescaffold list page is not working. I have ListLocations
I have a table view, with a long list of strings. There are headers
I have developed a simple contacts application and also implemented search using name. But
I have a JQuery Mobile (1.0rc1) application that has a list view with a
I have implemented a facebook style search suggestion feature for my application . But

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.