在活动中我有列表和大量,我需要在我的列表中搜索。我想要在ActionBar中搜索。我在ActionBar中创建了图标并尝试在其中进行搜索,但我的适配器有问题,我不知道如何使用getFilter()...这是活动代码:
public class Profile extends Activity {
...
private ListView prof;
private ArrayList<String> prof_item = new ArrayList<String>();
private String[] prof_inst;
MyProfieAdapter adapter;
RadioButton rbtn;
Menu menu;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile);
mSettings = getSharedPreferences(APP_PREFERENCES, Context.MODE_PRIVATE);
String mString = mSettings.getString(APP_PREFERENCES_NAME, null);
prof = (ListView) findViewById(R.id.profiles);
prof_inst = getResources().getStringArray(R.array.inst);
prof_item.add("11-101");
prof_item.add("11-102");
prof_item.add("11-201");
prof_item.add("11-202");
prof_item.add("11-203");
prof_item.add("11-204");
prof_item.add("11-205");
prof_item.add("11-301");
prof_item.add("11-302");
prof_item.add("11-303");
prof_item.add("11-304");
prof_item.add("11-305");
prof_item.add("11-306");
adapter = new MyProfieAdapter(Profile.this, R.layout.profile_item,
prof_item, prof_inst);
prof.setAdapter(adapter);
prof.setTextFilterEnabled(true);
prof.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
Log.d("List", "Position is = " + position);
Toast t = Toast.makeText(getApplicationContext(), "Position "
+ position, Toast.LENGTH_LONG);
t.show();
rbtn = (RadioButton) arg1.findViewById(R.id.radioButton1);
rbtn.setChecked(true);
SharedPreferences.Editor editor = mSettings.edit();
editor.putString(APP_PREFERENCES_NAME, prof_item.get(position)).commit();
editor.apply();
Log.d("Editor", editor.toString());
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(
R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
SearchView.OnQueryTextListener textChangeListener = new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String newText) {
// this is your adapter that will be filtered
adapter.getFilter().filter(newText);
System.out.println("on text change text: " + newText);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
// this is your adapter that will be filtered
adapter.getFilter().filter(query);
System.out.println("on query submit: " + query);
return true;
}
};
searchView.setOnQueryTextListener(textChangeListener);
}
return super.onOptionsItemSelected(item);
}
这是我的适配器列表:
public class MyProfieAdapter extends BaseAdapter {
Activity context;
ArrayList<String> items;
String[] items2;
ViewHolder holder;
private boolean userSelected = false;
private RadioButton mCurrentlyCheckedRB;
RadioButton radioBtn;
TextView text;
TextView inst;
private class ViewHolder {
RadioButton radioBtn;
TextView text;
TextView inst;
}
public MyProfieAdapter(Activity context, int profileItem, ArrayList<String> items,
String[] items2) {
super();
this.items = items;
this.items2 = items2;
this.context = context;
}
public View getView(final int position, View convertView, ViewGroup parent) {
holder = null;
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.profile_item, null);
holder = new ViewHolder();
radioBtn = (RadioButton) convertView
.findViewById(R.id.radioButton1);
radioBtn.setFocusable(false);
text = (TextView) convertView.findViewById(R.id.profile_text);
convertView.setTag(holder);
inst = (TextView) convertView.findViewById(R.id.textgroup);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
if (position == getCount() - 1 && userSelected == false) {
mCurrentlyCheckedRB = radioBtn;
} else {
radioBtn.setChecked(false);
}
radioBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mCurrentlyCheckedRB != null) {
mCurrentlyCheckedRB.setChecked(true);
Log.d("ListSelect", "Position is " + position + " "
+ items.get(position));
}
if (mCurrentlyCheckedRB == v)
return;
mCurrentlyCheckedRB.setChecked(false);
((RadioButton) v).setChecked(true);
mCurrentlyCheckedRB = (RadioButton) v;
}
});
text.setText(items.get(position));
inst.setText(items2[position]);
return convertView;
}
请帮帮我。我没有任何想法......