0

我的应用程序中有一个列表视图,如下所示:

--img-- --ArrayListItems(names)--- ---TextView----

我正在使用带有搜索视图的操作栏 sherlock 来使应用程序反向兼容。我正在使用扩展基本适配器并实现可过滤的自定义适配器。我真正需要的是,当用户在搜索栏中输入文本时,我应该能够根据 ArrayListItems(names) 过滤列表视图。我在stackoverflow上引用了几篇文章,尝试了所有代码片段。但我仍然无法搜索。当我运行代码时,搜索栏什么也不做。

在我的活动中,我做了:

public class SearchViewActionBar extends SherlockActivity implements SearchView.OnQueryTextListener 
{
    public SearchView mSearchView;
    private TextView mStatusView;
    int mSortMode = -1;


    protected CharSequence[] _options = { "Wild Life", "River", "Hill Station", "Temple", "Bird Sanctuary", "Hill", "Amusement Park"};
    protected boolean[] _selections =  new boolean[ _options.length ];


    private final String[] mStrings = Cheeses.sCheeseStrings;

    ListView lv1; 

    ItemListBaseAdapter adapter;

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

        setContentView(R.layout.activity_main);


       ArrayList<ItemDetails> image_details = GetSearchResults();
       lv1= (ListView) findViewById(R.id.list_view);

       adapter=new ItemListBaseAdapter(this, image_details);

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


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


        /*lv1.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> a, View v, int position, long id) { 
                Object o = lv1.getItemAtPosition(position);
                ItemDetails obj_itemDetails = (ItemDetails)o;
                //Toast.makeText(ListViewImagesActivity.this, "You have chosen : " + " " + obj_itemDetails.getName(), Toast.LENGTH_LONG).show();
            }  
        });*/


    }


    private ArrayList<ItemDetails> GetSearchResults()
    {
        ArrayList<ItemDetails> results = new ArrayList<ItemDetails>();


        for(int i=0;i<mStrings.length;i++)
        {

        ItemDetails item_details = new ItemDetails();
        item_details.setName(mStrings[i]);
        item_details.setItemDescription("200");
        results.add(item_details);
        }


        return results;
    }

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

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        super.onCreateOptionsMenu(menu);

        MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.searchview_in_menu, menu);
        MenuItem searchItem = menu.findItem(R.id.action_search);
        mSearchView = (SearchView) searchItem.getActionView();

       setupSearchView();

        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) 
    {
        if (mSortMode != -1) 
        {
            //Drawable icon = menu.findItem(mSortMode).getIcon();
            //menu.findItem(R.id.action_sort).setIcon(icon);
        }
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
    {


    switch (item.getItemId()) {
        //case R.id.action_search:
            //finish();
            //getSupportActionBar().setDisplayShowHomeEnabled(false);
            //getSupportActionBar().setDisplayShowTitleEnabled(false);
            //System.out.println("search pressed");
            //break;
        case R.id.action_edit:
            System.out.println("category pressed");
            showDialog( 0 );
            //finish();
            break;
        default:
            //return super.onOptionsItemSelected(item);
            break;
        }

        /*
        String c="Category";
        if(s.equals(c))
        {
            System.out.println("same");
            ;
        }*/

        //Toast.makeText(this, "Selected Item: " + item.getTitle(), Toast.LENGTH_SHORT).show();
        return true;
    }

    protected Dialog onCreateDialog( int id ) 
    {
        return 
        new AlertDialog.Builder( this )
            .setTitle( "Category" )
            .setMultiChoiceItems( _options, _selections, new DialogSelectionClickHandler() )
            .setPositiveButton( "SAVE", new DialogButtonClickHandler() )
            .create();
    }


    public class DialogSelectionClickHandler implements DialogInterface.OnMultiChoiceClickListener
    {
        public void onClick( DialogInterface dialog, int clicked, boolean selected )
        {
            Log.i( "ME", _options[ clicked ] + " selected: " + selected );
        }
    }


    public class DialogButtonClickHandler implements DialogInterface.OnClickListener
    {
        public void onClick( DialogInterface dialog, int clicked )
        {
            switch( clicked )
            {
                case DialogInterface.BUTTON_POSITIVE:
                    printSelectedPlanets();
                    break;
            }
        }
    }

    protected void printSelectedPlanets()
    {
        for( int i = 0; i < _options.length; i++ ){
            Log.i( "ME", _options[ i ] + " selected: " + _selections[i] );
        }
    }

    public void onSort(MenuItem item) 
    {
        mSortMode = item.getItemId();
        invalidateOptionsMenu();
    }


    public boolean onQueryTextChange(String newText) 
    {


        /*ActionBar ab = getSupportActionBar(); 
        ab.setDisplayShowTitleEnabled(false); 
        ab.setDisplayShowHomeEnabled(false);*/

        //getSupportActionBar().setDisplayShowHomeEnabled(false);
        //getSupportActionBar().setDisplayShowTitleEnabled(false);

        if (TextUtils.isEmpty(newText)) 
        {
            System.out.println("empty query");
            lv1.clearTextFilter();
        } 
        else 
        {
            //System.out.println(newText.toString());

            adapter.getFilter().filter(newText);
            //adapter.getFilter().filter(s);

            //lv1.setFilterText(newText.toString());
        }
        return true;
    }

    public boolean onQueryTextSubmit(String query) 
    {
        mStatusView.setText("Query = " + query + " : submitted");
        return false;
    }

    public boolean onClose() 
    {
        mStatusView.setText("Closed!");
        return false;
    }

    protected boolean isAlwaysExpanded() 
    {
        return false;
    }
}

我的适配器类:

public class ItemListBaseAdapter extends BaseAdapter implements Filterable
{
    private static ArrayList<ItemDetails> itemDetailsrrayList,list;

    private Integer[] imgid = {
            //R.drawable.p1,
            R.drawable.bb2,
            /*R.drawable.p2,
            R.drawable.bb5,
            R.drawable.bb6,
            R.drawable.d1*/
            };

    private LayoutInflater l_Inflater;

    public ItemListBaseAdapter(Context context, ArrayList<ItemDetails> results) {
        itemDetailsrrayList = results;
        l_Inflater = LayoutInflater.from(context);
    }

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

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

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

    public View getView(int position, View convertView, ViewGroup parent) 
    {
        ViewHolder holder;
        if (convertView == null) {
            convertView = l_Inflater.inflate(R.layout.item_details_view, null);
            holder = new ViewHolder();
            holder.txt_itemName = (TextView) convertView.findViewById(R.id.textView1);
            holder.txt_itemDescription = (TextView) convertView.findViewById(R.id.textView2);
            //holder.txt_itemPrice = (TextView) convertView.findViewById(R.id.phone);
            holder.itemImage = (ImageView) convertView.findViewById(R.id.imageView1);

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

        holder.txt_itemName.setText(itemDetailsrrayList.get(position).getName());
        //holder.txt_itemDescription.setText(itemDetailsrrayList.get(position).getItemDescription());
        holder.txt_itemDescription.setText("200");

        //holder.txt_itemPrice.setText(itemDetailsrrayList.get(position).getPrice());
        //holder.itemImage.setImageResource(imgid[itemDetailsrrayList.get(position).getImageNumber() - 1]);

        holder.itemImage.setImageResource(imgid[0]);

        return convertView;
    }

    static class ViewHolder 
    {
        TextView txt_itemName;
        TextView txt_itemDescription;
        //TextView txt_itemPrice;
        ImageView itemImage;
    }

    public Filter getFilter() 
    {
        Filter filter = new Filter() {

            @SuppressWarnings("unchecked")
            @Override
            protected void publishResults(CharSequence constraint,FilterResults results) {

                list = (ArrayList<ItemDetails>) results.values; // has the filtered values
                notifyDataSetChanged();  // notifies the data with new filtered values
            }

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults results = new FilterResults();        // Holds the results of a filtering operation in values
                List<String> FilteredArrList = new ArrayList<String>();

                if (list == null) {
                    list = new ArrayList<ItemDetails>(list); // saves the original data in mOriginalValues
                }

                /********
                 * 
                 *  If constraint(CharSequence that is received) is null returns the mOriginalValues(Original) values
                 *  else does the Filtering and returns FilteredArrList(Filtered)  
                 *
                 ********/
                if (constraint == null || constraint.length() == 0) {

                    // set the Original result to return  
                    results.count = list.size();
                    results.values = list;
                } else {
                    constraint = constraint.toString().toLowerCase();
                    for (int i = 0; i < list.size(); i++) 
                    {
                        String data="b";
                        //= list.getName(i);
                        if (data.toLowerCase().startsWith(constraint.toString())) {
                            FilteredArrList.add(data);
                        }
                    }
                    // set the Filtered result to return
                    results.count = FilteredArrList.size();
                    results.values = FilteredArrList;
                }
                return results;
            }
        };
        return filter;
    }

}
4

2 回答 2

2

您必须将 TextWatcher 添加到您的 searchView 中,如下所示:

mSearchView.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    adapter.getFilter().filter(s);
}}

笔记:

Filter 使用 ItemDetails 类的 toString 方法。您必须覆盖 toString 进行过滤。

于 2012-11-27T11:07:59.983 回答
1

我在这里发布了一个答案:https ://stackoverflow.com/a/12363961/450534这是基于我几天前的一个问题:Filtering a ListView with Baseadapter filters text not images

我在问题中面临的问题是文本会被图像过滤,不匹配。发布的答案解决了这个问题。正如答案中提到的那样,该解决方案适用于 aGridView但也适用于 a ListView

这是一个非常冗长的答案,所以我只是链接而不是在这里重新发布。顺便说一句,它拥有完整的解决方案,应该可以帮助您。

于 2012-11-28T07:46:53.487 回答