我有一个Fragment
,其中包含自定义ListView
的自定义Adapter
。我在此列表中显示的是一些医生,其属性为:picture
,name
,age
和gender
。
我想做的是按下按钮时按gender
过滤医生。这样默认会显示所有医生,男按钮会显示所有男性,以及一个女按钮所有女性。 (如下图所示)
这是我的片段代码:
public class Doctors extends Fragment {
String[] names = {"Richard Johnson", "William Perez", "Jennifer Fields", "Caroline Marin", "John Smith"};
String[] ages = {"25", "32", "41", "31", "29"};
String[] gender = {"Male", "Male", "Female", "Female", "Male"};
int[] images = { R.drawable.delete_doctora,R.drawable.delete_doctora,R.drawable.delete_doctora,R.drawable.delete_doctora,R.drawable.delete_doctora,};
ListView list;
ArrayAdapter<DocItem> adapter;
ArrayList<DocItem> arrayList;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState){
View view = inflater.inflate(R.layout.activity_doctors, container, false);
arrayList = new ArrayList<DocItem>();
for(int i = 0; i < 5; i++){
arrayList.add(i,new DocItem(names[i], ages[i], gender[i], images[i]));
}
adapter = new DocAdapter(getActivity(), arrayList);
list = (ListView) view.findViewById(R.id.ListDocs);
list.setAdapter(adapter);
list.setTextFilterEnabled(true);
return view;
}
}
我的自定义适配器:
public class DocAdapter extends ArrayAdapter<DocItem> {
private Context context;
private ArrayList<DocItem> _values;
public DocAdapter(Context context, ArrayList<DocItem> values) {
super(context, 0, values);
this.context = context;
_values = new ArrayList<DocItem>();
_values.addAll(values);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.doc_list_row, parent, false);
TextView txtName = (TextView) rowView.findViewById(R.id.Name);
TextView txtAge = (TextView) rowView.findViewById(R.id.Age);
ImageView imageView = (ImageView) rowView.findViewById(R.id.Pic);
txtName.setText(_values.get(position)._name);
txtAge.setText(_values.get(position)._age);
imageView.setImageResource(_values.get(position)._picture);
return rowView;
}
}
项目:
public class DocItem {
public String _name, _age, _gender;
int _picture;
public DocItem(String name, String age, String gender, int picture){
_name = name;
_age = age;
_picture= picture;
_gender = gender;
}
}
答案 0 :(得分:0)
在适配器中使用两个数组。使用一个用于存储,一个用于当前显示的项目。
private ArrayList<DocItem> all_values; //All items
private ArrayList<DocItem> filter_values; //Shown items
public DocAdapter(Context context, ArrayList<DocItem> values) {
...
all_values = new ArrayList<DocItem>();
all_values.addAll(values);
filter_values = all_values.clone();
}
...
@Override
public int getCount() {
return filter_values.size();
}
public void noFilter() {
filter_values = all_values;
notifyDataSetChanged();
}
public void filterMales() {
filterGender("Male");
}
public void filterFemales() {
filterGender("Female");
}
private void filterGender(String gender) {
filter_values = new ArrayList<>();
for(DocItem doc : all_values) {
if(doc._gender.equals(gender)) {
filter_values.add(doc);
}
}
notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
!!! USE filter_values array
}
用法:
在你的片段onCreateView:
Button buttonMales = (Button) findViewById(R.id.filterMales);
Button buttonFemales = (Button) findViewById(R.id.filterFemales);
buttonMales.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
adapter.filterMales();
}
});
buttonFemales.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
adapter.filterFemales();
}
});