"ClassName"文字列属性(Warrior、Hunter、Assassinなど)を持つ80以上の "God"オブジェクトのリストを持つAndroidアプリを開発しています。 )。ToggleButtonフィルタ付きListViewアダプタ
私はClassNameでリストをフィルタリングするToggleButtonsを実装しようとしています。一度に複数のフィルタを使用することができます(例: "Warrior"と "Hunter"を確認するToggleButtonsはClassName "Warrior"または "Hunter"を持つすべてのオブジェクトを表示する必要があります)。
これまでのところ、1つのClassName制約に基づいてリストをフィルタリングすることしかできませんでした。これを達成するための助けがあれば幸いです。ここで
は私のメインの断片である:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View myView = inflater.inflate(R.layout.home,null);
final ArrayList<God> gods = getGods();
lv = (ListView) myView.findViewById(R.id.home_list);
sv = (SearchView) myView.findViewById(R.id.home_search);
sv.setQueryHint("Search Gods...");
mage = (ToggleButton) myView.findViewById(R.id.mage);
assassin = (Button) myView.findViewById(R.id.assassin);
adapter = new GodAdapter(getActivity(), gods);
lv.setAdapter(adapter);
mage.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
//Include all mages
}
else {
//Remove all mages
}
}
});
assassin.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
//Include all assassins
}
else {
//Remove all assassins
}
}
});
(...other buttons...)
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
public boolean onQueryTextSubmit(String query) {
return false;
}
public boolean onQueryTextChange(String query) {
adapter.getFilter().filter(query);
return false;
}
});
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
String name = adapter.getItem(position).getName();
for(int i = 0; i < gods.size(); i++) {
if(gods.get(i).getName().equals(name))
{
Fragment myFragment = new TabFragment();
myFragment.setArguments(createGodBundle(gods.get(i)));
replaceFragment(myFragment);
hideSoftKeyboard(getActivity());
break;
}
}
}
});
return myView;
}
そして、ここでは私のGodAdapterです:
public class GodAdapter extends BaseAdapter implements Filterable {
Context c;
ArrayList<God> gods;
ArrayList<God> filterList;
CustomFilter filter;
public GodAdapter(Context c, ArrayList<God> gods) {
this.c = c;
this.gods = gods;
this.filterList = gods;
}
public int getCount() {
return gods.size();
}
public God getItem(int position) {
return gods.get(position);
}
public long getItemId(int position) {
return gods.indexOf(getItem(position));
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView == null)
convertView = inflater.inflate(R.layout.god_selection, null);
TextView name = (TextView) convertView.findViewById(R.id.god_name);
ImageView image = (ImageView) convertView.findViewById(R.id.god_image);
ImageView pantheon =(ImageView) convertView.findViewById(R.id.pantheon);
ImageView type =(ImageView) convertView.findViewById(R.id.type);
name.setText(gods.get(position).getName());
image.setImageResource(gods.get(position).getImage());
pantheon.setImageResource(gods.get(position).getPantheonIcon());
type.setImageResource(gods.get(position).getClassIcon());
return convertView;
}
public Filter getFilter() {
if(filter == null)
filter = new CustomFilter();
return filter;
}
//Inner class for filtering
class CustomFilter extends Filter {
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if(constraint != null && constraint.length() > 0)
{
constraint = constraint.toString().toUpperCase();
ArrayList<God> filters = new ArrayList<God>();
for(int i = 0; i < filterList.size(); i++)
{
if(filterList.get(i).getName().toUpperCase().contains(constraint))
{
God god = new God(filterList.get(i).getName(), filterList.get(i).getTitle(), filterList.get(i).getNameString(), filterList.get(i).getImage(),
filterList.get(i).getPantheon(), filterList.get(i).getPantheonIcon(), filterList.get(i).getClassName(), filterList.get(i).getClassIcon(), filterList.get(i).getType(),
filterList.get(i).getHealth(), filterList.get(i).getMana(), filterList.get(i).getDamage(), filterList.get(i).getProtPhys(), filterList.get(i).getProtMag(), filterList.get(i).getSpeed(),
filterList.get(i).getHp5(), filterList.get(i).getMp5(), filterList.get(i).getAttackSpeed());
filters.add(god);
}
}
results.count = filters.size();
results.values = filters;
} else {
results.count = filterList.size();
results.values = filterList;
}
return results;
}
protected void publishResults(CharSequence constraint, FilterResults results) {
gods = (ArrayList<God>) results.values;
notifyDataSetChanged();
}
}
}
これは私の問題を解決しました!あなたの助けをありがとう –