简体   繁体   中英

C# List Issue (Android/Monodroid ListView Search)

I am using Monodroid for Android Development in C#.

I am trying to create a search function. If the user types a search term it works, however when the backspace is pressed and nothing is in the text box (strTheFilter == ""), my listview is not being updated.

From debugging I can see that m_lCallSigns Count is zero when the user removes the search term, so its not adding anything. The bNeedToFilter has been used when the search is nothing. Why is it doing this? It is initialized in the constructor of this class and the function is called after initialization.

ListAdapter.cs  

    public void DisplayNewData(List<CallSignItem> new_items)
    {
        m_lCallSigns=new_items;
        NotifyDataSetChanged();
    }

    public void GetCallSignsFiltered(String strTheFilter, bool bNeedToFilter)
    {
        strTheFilter = strTheFilter.ToLower();

        List<CallSignItem> lFiltered = new List<CallSignItem>();

        foreach (CallSignItem item in m_lCallSigns)
        {
            if (item.strCallSign.ToLower().Contains(strTheFilter)
                || !bNeedToFilter)
            {
                lFiltered.Add(item);
            }
        }

        DisplayNewData(lFiltered);
    }

The function is called from my activity class.

Activity.cs 


        /// <summary>
        /// Updates the listview with search criteria (Action Listener).
        /// </summary>
        /// <param name="sender">Sender.</param>
        /// <param name="args">Arguments.</param>
        private void InputSearchOnTextChanged(object sender, System.EventArgs args)
        {
            String strSearch = m_etSearch.Text;

            bool bNeedToFilter = true;

            // If it's empty, we need the full list
            if (strSearch.Count() == 0)
            {
                bNeedToFilter = false;
            }

            // The EditText has a limit of 7 chars defined
            // in xml. Tell the user
            if (strSearch.Count() >= 7) 
            {
                Utils.DisplayDialogMessage ("Search", 
                    "You cannot enter more than 7 characters.",
                    true,
                    "",
                    "Ok",
                    false,
                    this);
            }  

            listAdapter.GetCallSignsFiltered(strSearch, bNeedToFilter);
        }

Every time you filter, you are passing the filtered list to DisplayNewData()

DisplayNewData(lFiltered);

public void DisplayNewData(List<CallSignItem> new_items)
{
    m_lCallSigns=new_items;
    NotifyDataSetChanged();
}

which then assigns the filtered list to m_lCallSigns. m_lCallSigns is also the "master" list you are using when filtering. You need to keep two copies, one unfiltered master copy, and one filtered copy that is used for display.

Thankyou Jason! Issue resolved.

I took a copy of the list.

public List<CallSignItem> m_lCallSigns_copy;

m_lCallSigns_copy = new List<CallSignItem>(m_lCallSigns);

Loop through the copied list so no index exceptions will ever be thrown and we are always reading the initial populated values:

            foreach (CallSignItem item in m_lCallSigns_copy)
            {
                if (item.strCallSign.ToLower().Contains(strTheFilter))
                {
                    lFiltered.Add(item);
                }
            }

Then update:

public void DisplayNewData(List<CallSignItem> new_items)
        {
            m_lCallSigns = new_items;
            NotifyDataSetChanged();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM