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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:08:03+00:00 2026-05-28T14:08:03+00:00

I am having a problem in searching a ListView. In my activity I have

  • 0

I am having a problem in searching a ListView. In my activity I have displayed some data in a list, Now I have a search bar on top of that list. I wanted to implement that when I type in the search bar the data in the listview should adjust according to the search results. I have used TextWatcher but the problem is the ArrayList from which I am populating the data in the list view is of custom type.

This is the custom type.

public class FRNews {

// Private Members START
        private long id  = 0;
        private String headline = "";
        private String detail = "";
        private String submit_date = Configuration.DEFAULT_DATE;

//Private Members END


// Getter/Setter START
        public void setId(long id){
            this.id = id ;
        }

        public void setHeadline(String headline){
            this.headline = headline ;
        }

        public void setDetail(String detail){
            this.detail = detail;
        }

        public void setSubmitDate(String submit_date){
            this.submit_date = submit_date;
        }

        public long getId(){
            return this.id ;
        }

        public String getHeadline(){
            return this.headline ;
        }

        public String getDetail(){
            return this.detail;
        }

        public String getSubmitDate(){
            return this.submit_date;
        }

// Getter/Setter END        

}

This is my class, which is calling the array adapter and is searching the listview using TextWatcher.

public class FRNewsList extends ListActivity {
    public static ArrayList<FRNews> newsList = new ArrayList<FRNews>();
    public static ArrayList<FRNews> tempList = new ArrayList<FRNews>();

    private int detailId = 0;
    private ProgressDialog progDialog;
    private FRCustomAdapterLatestNews newsAdapter;

    private ListView listView;
    private EditText etSearch;

    private TextView tvSeeAllNews;

    private String headline;

    int textLength = 0;

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

        setContentView(R.layout.tab_home_main);

        listView = (ListView) findViewById(android.R.id.list);
        etSearch = (EditText) findViewById(R.id.et_search_news);

        tvSeeAllNews = (TextView) findViewById(R.id.tv_news_see_all);

        if (newsList == null) {
            newsList = new ArrayList<FRNews>();
        }
        if (tempList == null) {
            tempList = new ArrayList<FRNews>();
        }

        putNewsInView();

        // listView.setAdapter(new ArrayAdapter<FRNews>(this,
        // android.R.layout.simple_list_item_1, tempList));

        listView.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View view,
                    int position, long id) {
                setDetailId(position);

                int newsId = getDetailId();

                Intent i = new Intent(FRNewsList.this,
                        FRLatestNewsDetailActivity.class);
                i.putExtra("id", newsId);

                // Create the view using FirstGroup's LocalActivityManager
                View v = FRNewsDetailActivityGroup.group
                        .getLocalActivityManager()
                        .startActivity("news_detail",
                                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
                        .getDecorView();

                // Again, replace the view
                FRNewsDetailActivityGroup.group.replaceView(v);

            }
        });

        etSearch.addTextChangedListener(new TextWatcher() {

            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {

                getNewsDbContent();
                textLength = count;
                tempList.clear();
                // int a = newsList.size();
                if (s.length() > 0) {
                    for (int i = 0; i < newsList.size(); i++) {
                        FRNews searchNews = newsList.get(i);
                        String headlineSearch = searchNews.getHeadline();
                        if (textLength <= headlineSearch.length()) {
                            if (etSearch
                                    .getText()
                                    .toString()
                                    .equalsIgnoreCase(
                                            (String) headlineSearch
                                                    .subSequence(0, textLength))) {
                                tempList.add(i, searchNews);
                            }
                        }
                    }
                    listView.setAdapter(new ArrayAdapter<FRNews>(
                            FRNewsList.this,
                            android.R.layout.simple_list_item_1, tempList));
                } else
                    listView.setAdapter(new ArrayAdapter<FRNews>(
                            FRNewsList.this,
                            android.R.layout.simple_list_item_1, newsList));
            }

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

            public void afterTextChanged(Editable s) {
            }
        });

        tvSeeAllNews.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Intent i = new Intent(FRNewsList.this, FRAllNews.class);

                // Create the view using FirstGroup's LocalActivityManager
                View view = FRNewsDetailActivityGroup.group
                        .getLocalActivityManager()
                        .startActivity("news_all",
                                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
                        .getDecorView();

                // Again, replace the view
                FRNewsDetailActivityGroup.group.replaceView(view);
            }
        });
    }

    private void putNewsInView() {
        // public void run() {
        getNewsDbContent();
        newsAdapter = new FRCustomAdapterLatestNews(getApplicationContext(),
                R.layout.tab_home_row, R.id.tv_news_date, tempList);

        setListAdapter(newsAdapter);
    }

    private void getNewsDbContent() {
        newsList.clear();
        tempList.clear();
        FRNewsTable getNews = new FRNewsTable(FRNewsList.this);
        newsList = getNews.getNewsFromDB("news_id,headline,detail,submit_date");
        // setNewsList(newsList);
        tempList = newsList;
        getNews.CloseDB();

    }

    public int getDetailId() {
        return detailId;
    }

    public void setDetailId(int detailId) {
        this.detailId = detailId;
    }

}

I am not getting the size of my ArrayList as I am using newsList.size() while debugging.
Please review this code and help me out in this.

Thanks in advance.

  • 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-28T14:08:04+00:00Added an answer on May 28, 2026 at 2:08 pm

    Try writing your code inside TextChangedListener

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

Sidebar

Related Questions

I have been having a problem that has troubled me for a while. Searching
I've been thinking/searching for an equivalent of the problem that I am having below,
We are having problem with the server migration. We have one application that are
I'm having a problem and im tired of searching for the answer.. I have
I'm having some problem searching by group when using Sunspot. Here is an example:
I'm having this problem : I have some divs wich are animated with jquery,
Having a small problem for a quick Search and Highlight script that I'm working
I'm having problem when running my Windows Forms program. In the program, I have
I am having an odd problem when searching a UITableView using a UISearchDisplayController. The
I am having a problem regarding a Log Searching Speed and Disk Size. It

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.