0

尝试获取搜索功能以过滤列表视图中的片段时遇到问题。我正在按照本教程使用 ActionBarSherlock: http ://www.androidbegin.com/tutorial/implementing-actionbarsherlock-search-collapsible-view-in-android/ 。到目前为止,我在 MainActivity.java 上有搜索功能/搜索按钮。我已经尝试将它移动到片段中,正如它从教程中显示的那样,但它没有显示在操作栏中。

编辑:添加 Populate.java 和 NavListAdapter.java代码

MainActivity.java

public class MainActivity extends SherlockFragmentActivity {
// Declare Variables
NavListAdapter adapter;
EditText editsearch;
String[] title;
String[] subtitle;
Fragment fragment1 = new FiveHundred();
Fragment fragment2 = new OneThousand();


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

// Generate title
title = new String[] { "Title Fragment 1", "Title Fragment 2" };

// Generate subtitle
subtitle = new String[] { "Subtitle Fragment 1", "Subtitle Fragment 2" };


// Pass results to NavListAdapter Class
adapter = new NavListAdapter(this, title, subtitle);

// Hide the ActionBar Title
getSupportActionBar().setDisplayShowTitleEnabled(false);

// Create the Navigation List in your ActionBar
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

// Listen to navigation list clicks
ActionBar.OnNavigationListener navlistener = new OnNavigationListener() {

    @Override
    public boolean onNavigationItemSelected(int position, long itemId) {
        FragmentTransaction ft = getSupportFragmentManager()
                .beginTransaction();
        // Locate Position
        switch (position) {
        case 0:
            ft.replace(android.R.id.content, fragment1);
            break;
        case 1:
            ft.replace(android.R.id.content, fragment2);
            break;
        }
        ft.commit();
        return true;
    }

};
// Set the NavListAdapter into the ActionBar Navigation
getSupportActionBar().setListNavigationCallbacks(adapter, navlistener);
}

// Create the options menu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Get the options menu view from menu.xml in menu folder
getSupportMenuInflater().inflate(R.menu.search_layout, menu);

// Locate the EditText in menu.xml
editsearch = (EditText) menu.findItem(R.id.menu_search).getActionView();

// Capture Text in EditText
editsearch.addTextChangedListener(textWatcher);

// Show the search menu item in menu.xml
MenuItem menuSearch = menu.findItem(R.id.menu_search);

menuSearch.setOnActionExpandListener(new OnActionExpandListener() {

    // Menu Action Collapse
    @Override
    public boolean onMenuItemActionCollapse(MenuItem item) {
        // Empty EditText to remove text filtering
        editsearch.setText("");
        editsearch.clearFocus();
        return true;
    }

    // Menu Action Expand
    @Override
    public boolean onMenuItemActionExpand(MenuItem item) {
        // Focus on EditText
        editsearch.requestFocus();

        // Force the keyboard to show on EditText focus
        InputMethodManager imm = (InputMethodManager)
        getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
        return true;
    }
});

return true;
}

// EditText TextWatcher
private TextWatcher textWatcher = new TextWatcher() {

@Override
public void afterTextChanged(Editable s) {
    // TODO Auto-generated method stub
    String text = editsearch.getText().toString()
            .toLowerCase(Locale.getDefault());
    adapter.filter(text);
}

@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
        int arg3) {
    // TODO Auto-generated method stub

}

@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
        int arg3) {
    // TODO Auto-generated method stub

}

};
}

ListViewAdapter.java

public class ListViewAdapter extends BaseAdapter
{
// Declare Variables
Context mContext;
LayoutInflater inflater;
private List<Populate> populatelist = null;
private ArrayList<Populate> arraylist;

public ListViewAdapter(Context context, List<Populate> populatelist) 
{
mContext = context;
this.populatelist = populatelist;
inflater = LayoutInflater.from(mContext);
this.arraylist = new ArrayList<Populate>();
this.arraylist.addAll(populatelist);
}

public class ViewHolder 
{
TextView Name;
TextView Value;

}

@Override
public int getCount() 
{
    return populatelist.size();
}

@Override
public Populate getItem(int position) 
{
    return populatelist.get(position);
}

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

public View getView(final int position, View convertView, ViewGroup parent) 
{
    final ViewHolder holder;
    if (convertView == null) 
{
    holder = new ViewHolder();
    convertView = inflater.inflate(R.layout.listview_item, null);
    // Locate the TextViews in listview_item.xml
    holder.Name = (TextView) convertView.findViewById(R.id.Name);
    holder.Value = (TextView) convertView.findViewById(R.id.Value);
    convertView.setTag(holder);
}
else 
{

    holder = (ViewHolder) convertView.getTag();
}
// Set the results into TextViews
holder.Name.setText(populatelist.get(position).getName());
holder.Value.setText(populatelist.get(position).getValue());     

// Listen for ListView Item Click
convertView.setOnClickListener(new OnClickListener() 
{
    public void onClick(View arg0) 
    {
        // Send single item click data to SingleItemView Class
        Intent intent = new Intent(mContext, SingleItemView.class);
        // Pass all data errorName
        intent.putExtra("Name",(populatelist.get(position).getName()));
        // Pass all data errorCode
        intent.putExtra("Value",(populatelist.get(position).getValue()));
        // Start SingleItemView Class
        mContext.startActivity(intent);
    }

});
    return convertView;
}

// Filter Class
public void filter(String charText) 
{
    charText = charText.toLowerCase(Locale.getDefault());
    populatelist.clear();
    if (charText.length() == 0) 
{
    populatelist.addAll(arraylist);
}
else
{
    for (Populate p : arraylist)
    {
        // Search filter code
        if (p.getName().toLowerCase(Locale.getDefault()).contains(charText))
        {
            populatelist.add(p);
        }
        else if (p.getValue().toLowerCase(Locale.getDefault()).contains(charText))
        {
            populatelist.add(p);
        }
    }

}       
notifyDataSetChanged();
}

}

五百.java

public class FiveHundred extends SherlockFragment
{
// Declare Variables
ListView list;
ListViewAdapter adapter;
EditText editsearch;
String[] Name;
String[] Value;

ArrayList<Populate> arraylist = new ArrayList<Populate>();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) 
{
     View rootView = inflater.inflate(R.layout.listview_main, container, false);
     return rootView;
}

@Override
public void onStart()
{
     arraylist.clear();
     super.onStart();

// Generate sample data
Name = new String[] 
        { 
            // 0-10
            "A name goes here!!", "Mother", "Father" 
        };

Value = new String[] 
        { 
            // 0-10
            "A value goes here!!", "1", "2"
        };


// Locate the ListView in listview_main.xml
list = (ListView) getView().findViewById(R.id.listview);

for (int i = 0; i < Name.length; i++)
{
    Populate p = new Populate(Name[i], Value[i]);
    // Binds all Strings into an array
    arraylist.add(p);
}

// Pass results to ListViewAdapter.class
adapter = new ListViewAdapter(getActivity(), arraylist);

// Binds the adapter to the ListView
list.setAdapter(adapter);
}

}

填充.java

public class Populate 
{
private String Name;
private String Value;

public Populate(String Name, String Value) 
{
this.Name = Name;
this.Value = Value;
}

public String getName() 
{
return this.Name;
}

   public String getValue() 
   {
     return this.Value;
   }
   }

NavListAdapter.java

public class NavListAdapter extends BaseAdapter {

// Declare Variables
Context context;
String[] mTitle;
String[] mSubTitle;
LayoutInflater inflater;

public NavListAdapter(Context context, String[] title, String[] subtitle) {
this.context = context;
this.mTitle = title;
this.mSubTitle = subtitle;
}

@Override
public int getCount() {
    return mTitle.length;
}

@Override
public Object getItem(int position) {
    return mTitle[position];
}

@Override
public long getItemId(int position) {
    return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// Declare Variables
TextView txtTitle;
TextView txtSubTitle;

inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.nav_list_item, parent, false);

// Locate the TextViews in nav_list_item.xml
txtTitle = (TextView) itemView.findViewById(R.id.title);
txtSubTitle = (TextView) itemView.findViewById(R.id.subtitle);

// Set the results into TextViews
txtTitle.setText(mTitle[position]);
txtSubTitle.setText(mSubTitle[position]);
return itemView;
}

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
// Declare Variables
TextView txtTitle;
TextView txtSubTitle;

inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dropdownView = inflater.inflate(R.layout.nav_dropdown_item, parent,
        false);

// Locate the TextViews in nav_dropdown_item.xml
txtTitle = (TextView) dropdownView.findViewById(R.id.title);
txtSubTitle = (TextView) dropdownView.findViewById(R.id.subtitle);

// Set the results into TextViews
txtTitle.setText(mTitle[position]);
txtSubTitle.setText(mSubTitle[position]);

return dropdownView;
}

public void filter(String text) {
// TODO Auto-generated method stub

}

}
4

1 回答 1

0

你需要确保你调用setHasOptionsMenu(true);内部onCreateonCreateView你的两个片段。

参考: setHasOptionsMenu

您还需要onCreateOptionsMenu在两个片段内部实现覆盖。

   @Override
public void onCreateOptionsMenu (Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.search_layout, menu);
    super.onCreateOptionsMenu(menu, inflater);
}

我希望它会有所帮助!

于 2013-10-11T04:57:58.110 回答