我想在列表视图中使用适配器类添加搜索栏,任何人都可以帮我下面是我的代码。 这是我正在使用的适配器类。 Cat_Headers_Home_Adapter.java
public class Cat_Headers_Home_Adapter extends SectionAdapter {
private Activity cntx;
private List<Categories> categoriesList;
private List<CatHeaders> categoriesHeadersList;
public Cat_Headers_Home_Adapter(Activity cntx,List<Categories>categoriesList, List<CatHeaders> categoriesHeadersList) {
this.cntx = cntx;
this.categoriesList = categoriesList;
this.categoriesHeadersList = categoriesHeadersList;
}
@Override
public int numberOfSections() {
return categoriesHeadersList.size();
}
@Override
public int numberOfRows(int section) {
return categoriesList.size();
}
@Override
public Object getRowItem(int section, int row) {
return null;
}
@Override
public boolean hasSectionHeaderView(int section) {
return true;
}
@Override
public View getRowView(int section, int row, View convertView, ViewGroup parent) {
View view = convertView;
Holder holder;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) cntx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.adapter_home_service_cat, null);
holder = new Holder();
view.setTag(holder);
} else {
holder = (Holder) view.getTag();
}
Categories categories = categoriesList.get(section);
holder.service_name = (TextView) view.findViewById(R.id.service_name);
holder.count = (TextView) view.findViewById(R.id.total);
holder.service_icon = (ImageView) view.findViewById(R.id.service_icon);
holder.service_layout = (LinearLayout)view.findViewById(R.id.service_layout);
holder.service_name.setText(categories.catName);
holder.count.setText(categories.count);
holder.service_icon.setImageResource(categories.catImage);
holder.service_layout.setVisibility(View.VISIBLE);
return view;
}
private class Holder {
TextView service_name;
TextView count;
ImageView service_icon;
LinearLayout service_layout;
}
@Override
public int getSectionHeaderViewTypeCount() {
return 2;
}
@Override
public int getSectionHeaderItemViewType(int section) {
return section % 2;
}
@Override
public View getSectionHeaderView(int section, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = (TextView) cntx.getLayoutInflater().inflate(cntx.getResources().getLayout(android.R.layout.simple_list_item_1), null);
}
((TextView) convertView).setText(categoriesHeadersList.get(section).catName);
((TextView) convertView).setTextColor(cntx.getResources().getColor(android.R.color.white));
convertView.setBackgroundColor(cntx.getResources().getColor(R.color.red));
return convertView;
}
@Override
public void onRowItemClick(AdapterView<?> parent, View view, int section, int row, long id) {
super.onRowItemClick(parent, view, section, row, id);
Toast.makeText(cntx, "Section " + section + " Row " + row, Toast.LENGTH_SHORT).show();
}
}
这是一个我想在其上添加搜索栏的班级 Home_fragment.java
public class Home_Fragment extends Fragment {
private HeaderListView list;
private EditText searchBar;
private int[] images;
private String[] array;
private String[] headers;
private String[] fake_count;
ArrayList<HashMap<String, String>> listView;
private List<Categories> categoriesList = new ArrayList<>();
private List<CatHeaders> categoriesListHeader = new ArrayList<>();
public static Home_Fragment newInstance(int index) {
Home_Fragment f = new Home_Fragment();
// Supply index input as an argument.
Bundle args = new Bundle();
args.putInt("position", index);
f.setArguments(args);
return f;
}
public int getShownIndex() {
return getArguments().getInt("index", 0);
}
public Home_Fragment() {
// Required empty public constructor
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listView = new ArrayList<>();
images = Utils.getResourcesImages();
// images = getActivity().getResources().getIntArray(R.array.service_images_for_list_headers);
array = getActivity().getResources().getStringArray(R.array.services);
headers = getActivity().getResources().getStringArray(R.array.header_title);
fake_count = getActivity().getResources().getStringArray(R.array.fake_count);
for (int a = 0; a < images.length; a++) {
int imageId = images[a];
String count = fake_count[a];
String name = array[a];
Categories categoriesObj;
if (a == 0 || a == 29 || a == 36 || a == 42 || a == 53) {
categoriesObj = new Categories(name, imageId, count, true);
} else {
categoriesObj = new Categories(name, imageId, count, false);
}
categoriesList.add(categoriesObj);
}
for (int a = 0; a < headers.length; a++) {
CatHeaders categoriesObj;
categoriesObj = new CatHeaders(headers[a]);
categoriesListHeader.add(categoriesObj);
}
Log.d("HOME Message Fragment", "Home Message Fragment");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
list = (HeaderListView) rootView.findViewById(R.id.list_view);
searchBar = (EditText) rootView.findViewById(R.id.search_field);
searchBar.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (s.length() > 2) {
searchServiceFromList(s.toString());
} else {
setOldView();
}
}
});
Arrays.sort(array);
listView.clear();
for (int i = 0; i < array.length; i++) {
HashMap<String, String> map = new HashMap<>();
map.put("service_name", array[i]);
map.put("icon_position", Integer.toString(i));
map.put("count", fake_count[i]);
listView.add(map);
}
Cat_Headers_Home_Adapter adapter = new Cat_Headers_Home_Adapter(getActivity(),categoriesList,categoriesListHeader);
list.setAdapter(adapter);
// list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
// @Override
// public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// HashMap<String, String> map = listView.get(position);
// // Toast.makeText(getActivity(), map.get("service_name").toString(), Toast.LENGTH_SHORT).show();
// // objBook.show();
// // objBook.setService(map.get("service_name").toString());
// /* Post_New_Job_Fragment fragment = new Post_New_Job_Fragment();
// Bundle bundle = new Bundle();
// bundle.putString("service_name", map.get("service_name").toString());
// fragment.setArguments(bundle);
// final FragmentTransaction ft = getFragmentManager().beginTransaction();
// ft.replace(R.id.container, fragment, "NewFragmentTag");
// ft.addToBackStack(null);
// ft.commit();*/
// Intent i = new Intent(getActivity(), Post_New_Job_Activity.class);
// i.putExtra("service_name", map.get("service_name").toString());
// i.putExtra("icon_position", map.get("icon_position").toString());
// getActivity().startActivityForResult(i, 200);
//
//
// }
// });
return rootView;
}
@Override
public void startActivityForResult(Intent intent, int requestCode) {
super.startActivityForResult(intent, requestCode);
}
public void searchServiceFromList(String s) {
// ArrayList<HashMap<String, String>> tempList = new ArrayList<>();
// for (int i = 0; i < listView.size(); i++) {
// HashMap<String, String> map = listView.get(i);
// if ((map.get("service_name").toString().toLowerCase()).contains(s.toLowerCase())) {
// tempList.add(map);
// }
// }
//
// Home_Adapter adapter = new Home_Adapter(getActivity(), tempList, images);
// list.setAdapter(adapter);
}`enter code here`
public void setOldView() {
// Home_Adapter adapter = new Home_Adapter(getActivity(), listView, images);
// list.setAdapter(adapter);
}
}`enter code here`