Hide ListView items in android and refresh onClick event

0

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?

    
asked by Miguel Angel Martinez Sanchez 03.12.2016 в 12:27
source

1 answer

1

The getView() method of an adapter should not be used to handle the logic of what is to be deployed, but only to inflate the view. The adapter is only a bridge between the data and the UI and should not be smarter than that. Take out the logic of what you are going to show about that method

@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);
    }         

    // Aquí debes hacer findViewById() como en una Actividad para cada elemento visual del item


    return convertView;
}

I'm not 100% sure where the logic that you put there should go but the safest thing is that you can do it within menuItemClicked() . Just remember that every time you hide an item, you have to remove it from items and call notifyDatasetChanged() , therefore you must also change:

@Override
public int getCount() {
    return this.items.size();
}

I would also recommend using the ViewHolder pattern because you use a ListView and can get to laggearse your UI.

    
answered by 03.12.2016 / 19:42
source