I want to hide the items according to a parameter in a ListView. I have done this and it works:
final ArrayList<? extends IMenuPrintable> adapterArray = sessionUser.getAvailableServicesForCategoryId(categoryId);
ListView listView = (ListView) findViewById(R.id.main_menu_list_view);
listView.setAdapter(new MenuAdapter(this, adapterArray));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
menuItemClicked(position);
}
});
The MenuAdapter class is:
public class MenuAdapter extends BaseAdapter {
private ArrayList<? extends IMenuPrintable> items;
private Context context;
private ArrayList<Integer> hiddenPositions = new ArrayList<>();
public MenuAdapter(Context context, ArrayList<? extends IMenuPrintable> items) {
this.context = context;
this.items = items;
if (this.items == null) {
this.items = new ArrayList<>();
}
}
@Override
public int getCount() {
return this.items.size() - hiddenPositions.size();
}
@Override
public Object getItem(int position) {
return this.items.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = ((Activity) context).getLayoutInflater().inflate(R.layout.main_menu_row, parent, false);
}
LDService service = (LDService) items.get(position);
if (service.type.equals("4")) {
hideItem(position);
for (Integer hiddenIndex : hiddenPositions) {
if (hiddenIndex <= position) {
position = position + 1;
}
}
}
return convertView;
}
private void hideItem(int itemToHide) {
if (!hiddenPositions.contains(itemToHide)) {
hiddenPositions.add(itemToHide);
}
}
private void menuItemClicked(int position) {
ArrayList<? extends IMenuPrintable> services = sessionUser.getAvailableServicesForCategoryId(categoryId)
LDService service = (LDService) services.get(position);
//para cada 'service' compruebo ciertos parámetros...
}
}
With this I manage to hide the items but the functionality does not exist. When I click on the first item that is displayed, it has the position of the item hidden. That is, the function 'menuitemclicked ()' is passed as a parameter to the position of the default item (the hidden item). I want to update that position. How could I do it?