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

  • Home
  • SEARCH
  • 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 6066421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:29:57+00:00 2026-05-23T09:29:57+00:00

Problem Create a ListView Enable filtering for the text view Make sure that the

  • 0

Problem

  • Create a ListView
  • Enable filtering for the text view
  • Make sure that the first item is disabled
  • Filter the view so that the first item is not shown

If you do this you can see that the new first item is disabled.

See the attached screenshots:

Before filtering

After filtering

The problem is that when the ListView filters actually changes the position of the items so the item that use to have position 10 now has position 0 which is what causes the problem.

So how do I best work around this?

Below is the smallest possible sample code that demonstrates the problem, just filter for something other than the first three items.

CustomListActivity.java

package com.example.bug;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

public class CustomListActivity extends ListActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String items[] = new String[100];

        for(int i = 0; i < items.length; ++i)
            items[i] = "Item " + (i+1);

        setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, items) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view = super.getView(position, convertView, parent);
                view.setEnabled(isEnabled(position));
                return view;
            }

            @Override
            public boolean areAllItemsEnabled() {
                return false;
            }

            @Override
            public boolean isEnabled(int position) {
                return position >= 3;
            }
        });

        getListView().setTextFilterEnabled(true);
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      android:versionCode="1"
      android:versionName="1.0" package="com.example.bug">
    <uses-sdk android:minSdkVersion="7" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name="CustomListActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

res/color/list_item_colors.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:color="#777" />
    <item android:color="#fff" />
</selector>

res/layout/list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="10sp"
    android:textSize="16sp"
    android:textColor="@color/list_item_colors" />
  • 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-05-23T09:29:57+00:00Added an answer on May 23, 2026 at 9:29 am

    If you know that no other part of your program will be using the tag associated with the Views that represent the items in the list, you could initialize this the first time getView() is called (when presumably the filter will be empty) and then use the value of the tag on later calls to determine whether or not the items are enabled. For example :

            public View getView(int position, View convertView, ViewGroup parent) {
                View view = super.getView(position, convertView, parent);
                Integer tag = (Integer) view.getTag();
                if (tag == null) {
                   tag = new Integer(position);
                   view.setTag(tag);
                }
                view.setEnabled(isEnabled(tag.intValue()));
                return view;
            }
    

    EDIT: Based on the replies below, if it’s possible to use something other than Strings as the array elements, I would recommend the following. Create a new class used to represent the elements –

     private class Element {
    
        private String myString;
    
        private int myIndex;
    
        @Override
        public String toString() {
            return myString;
        }       
    
    }
    

    then change the array from Strings to Elements :

        Element items[] = new Element[100];
    
        for(int i = 0; i < items.length; ++i)
        {
            items[i] = new Element();
            items[i].myString = "Item " + (i+1);
            items[i].myIndex = i;
        }
    

    Finally, change the ArrayAdapter declaration and getView method to the following :

         setListAdapter(new ArrayAdapter<Element>(this, R.layout.list_item, items) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view = super.getView(position, convertView, parent);
                view.setEnabled(isEnabled(getItem(position).myIndex));
                return view;
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ListView that uses different XML files to create Views and make
I have a problem with the Create View in the SimpleRepository example in Subsonic
I create new ASP.NET web application that use SMTP to send message. The problem
I am trying to create a ListView that will be populated with the entries
Problem I'm trying to create a ListView where you can reorder the items by
I'm attempting to create a listview that binds dynamically to a set of dates.
I managed to implement a GalleryView, create a custom Adapter that returns a ListView
I have a weird problem! I'm trying to create a listview with checkboxes. In
I'm trying to create a listview with a static background image (i.e. not contained
I have a listview which I populate from a webservice query. Problem is that

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.