Android操作栏使用基本适配器在片段列表中搜索

时间:2014-12-02 12:07:04

标签: android android-actionbar fragment

您好我正在使用android.I在操作栏中有一个搜索视图。我正在尝试在listview中使用 baseadapter 实现动作栏搜索。我搜索了很多但是所有教程提供有关ArrayAdapter的信息。如何使用 baseadapter 从片段内的列表视图中搜索动作栏。如何解决此问题?请帮我。在此先感谢:)。这是我的代码

 public class UpcomingEvents extends Fragment {
 ListView listView;

@SuppressLint("NewApi")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_events, container, false);
    setHasOptionsMenu(true);

 String[] date = new String[] { "January 25, 2013",
                "February 19, 2013", "Ferbuary 21, 2013", "February 24, 2013","March 10, 2013","April 15, 2013"};

 String[] descriptions = new String[] {
                "Annual meeting","Birthday celebration","Annual auditing","Christmas celebration ","Marriage function","Football tournament"
                };
 String[] place = new String[] { "Auditorium",
            "Auditorium", "Auditorium", "Library hall","Auditorium","School Ground"};

 String[] time = new String[] { "9 pm",
            "6 pm", "5 pm", "12 pm","10 am","8 pm"};

 getActivity().getActionBar().setDisplayShowTitleEnabled(false);
 getActivity().getActionBar().setHomeButtonEnabled(false);



 listView=(ListView)rootView.findViewById(R.id.listView1);
 listView.setAdapter(new dataListAdapter(date,descriptions,place,time));
 listView.setTextFilterEnabled(true);

    return rootView;
}
class dataListAdapter extends BaseAdapter {
    String[] Date, Desc,Loc, Time;

    dataListAdapter() {
        Date = null;
        Desc = null;
        Loc = null;
        Time = null;
    }

    public dataListAdapter(String[] text, String[] text1,String[] text3, String[] text4) {
        Date = text;
        Desc = text1;
        Loc = text3;
        Time = text4;

    }

    public int getCount() {
        // TODO Auto-generated method stub
        return Date.length;
    }

    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

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

        LayoutInflater inflater = getActivity().getLayoutInflater();
        View row;
        row = inflater.inflate(R.layout.event_item, parent, false);
        TextView date, desc,loc,time;

        date = (TextView) row.findViewById(R.id.date);
        desc = (TextView) row.findViewById(R.id.desc);
        loc = (TextView) row.findViewById(R.id.place);
        time = (TextView) row.findViewById(R.id.time);
        Typeface font = Typeface.createFromAsset(
                getActivity().getAssets(), 
                "Ace.ttf");
        date .setTypeface(font);
        date.setText(Date[position]);
        desc.setText(Desc[position]);
        loc.setText(Loc[position]);
        time.setText(Time[position]);


        return (row);
    }
}
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater =getActivity(). getMenuInflater();
    inflater.inflate(R.menu.main, menu);
     SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();

            searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
            searchView.setIconifiedByDefault(false);   

            SearchView.OnQueryTextListener textChangeListener = new SearchView.OnQueryTextListener()
               {
                   @Override
                   public boolean onQueryTextChange(String newText)
                   {
                       // this is your adapter that will be filtered
                       dataListAdapter.getFilter().filter(newText);
                       System.out.println("on text chnge text: "+newText);
                       return true;
                   }
                   @Override
                   public boolean onQueryTextSubmit(String query)
                   {
                       // this is your adapter that will be filtered
                       myAdapter.getFilter().filter(query);
                       System.out.println("on query submit: "+query);
                       return true;
                   }
               };
               searchView.setOnQueryTextListener(textChangeListener);

               return true;


}
@Override
  public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
          case R.id.filter:
                    Toast.makeText(getActivity(), "filter clicked", 5000).show();
              // put your code here to change the icon
              return true;

          default:
              return super.onOptionsItemSelected(item);
      }
  }

0 个答案:

没有答案