Data does not display properly in Adapter when Removing/Adding

78 Views Asked by At

I am running into an issue where I use 2 separate adapters, and 2 seperate GridViews (1 Adapter, for 1 GridView), and am basically removing items from either adapter, and adding it to the other if the item is pressed, and vice versa.

When I press and item in one GridView, the item will be removed from its respective adapter and put into the other GridView and its respective adapter. The issue seems to be with the way the data is being stored (potentially) because when I add the item to the adapter, the icons are totally different from what they should be.

Note: I also noticed that this only happens for the items pressed after the first one is pressed. So the first items data will be added/removed properly. However, I have found that when the item that takes on the first position of the associated adapter, will now have the icon of the previously removed item.

What can I do so ensure that the data stays consistent with the item that is pressed?.

Adapter Class

public class MiscChargeOptionsAdapter extends ArrayAdapter<CarClassSettingDetails> {

private LayoutInflater inflater;

public MiscChargeOptionsAdapter(Context context, List<CarClassSettingDetails> settingList) {
    super(context, R.layout.misc_charge_option_layout, R.id.option_grid_option_text, settingList);
    inflater = LayoutInflater.from(context);
}

@SuppressLint("InflateParams")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    CarClassSettingDetails settingsDetails = this.getItem(position);
    ViewHolder vh;
    if (convertView == null) {
        vh = new ViewHolder();
        convertView = inflater.inflate(R.layout.misc_charge_option_layout, null);
        vh.optionText = (TextView) convertView.findViewById(R.id.option_grid_option_text);
        vh.settingView = (SettingView) convertView.findViewById(R.id.option_grid_option_icon);
        convertView.setTag(vh);
    } else {
        vh = (ViewHolder) convertView.getTag();
    }

    vh.optionText.setText(settingsDetails.getName());

    vh.settingView.setIcon(settingsDetails.getIcon());
    vh.settingView.setIconSelected(settingsDetails.getIconSelected());
    vh.settingView.setViewBackgroundColor(settingsDetails.getViewBackgroundColor());
    vh.settingView.setBackgroundColorSelected(settingsDetails.getBackgroundColorSelected());
    vh.settingView.setAmountBackgroundColor(settingsDetails.getAmountBackgroundColor());
    vh.settingView.setAmountTextColor(settingsDetails.getAmountTextColor());
    vh.settingView.setValue(settingsDetails.getValue());
    vh.settingView.setIgnoreSetState(true);
    vh.settingView.setTag(position);
    return convertView;
}

class ViewHolder {
    private TextView optionText;
    private SettingView settingView;

    /*      public ViewHolder(TextView optionText, SettingView settingView) {
                this.optionText = optionText;
                this.settingView = settingView;
            }*/

    public TextView getOptionText() {
        return optionText;
    }

    public SettingView getSettingView() {
        return settingView;
    }
}

}

How the adapters are set

selectedAdapter = new CarClassOptionsAdapter(getActivity(), selectedSettingsList); 

unselectedAdapter = new CarClassOptionsAdapter(getActivity(), unselectedSettingsList);


    gridSelected.setAdapter(selectedAdapter); 
    gridSelected.setOnItemClickListener(this);
    gridUnselected.setAdapter(unselectedAdapter); 
    gridUnselected.setOnItemClickListener(this);

How I am adding/removing items from the adapters

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    if (parent.getAdapter() == selectedAdapter) {
        //TODO: ADD DIALOGS
        unselectedAdapter.add(selectedAdapter.getItem(position));
        unselectedAdapter.notifyDataSetChanged();
        selectedAdapter.remove(selectedAdapter.getItem(position));
        selectedAdapter.notifyDataSetChanged();

    } else if (parent.getAdapter() == unselectedAdapter) {
        //TODO: ADD DIALOGS
        selectedAdapter.add(unselectedAdapter.getItem(position));
        selectedAdapter.notifyDataSetChanged();
        unselectedAdapter.remove(unselectedAdapter.getItem(position));
        unselectedAdapter.notifyDataSetChanged();
    }
}

This is how the CarClassSettingDetails class is being populated

private void addMiscChargeSelected(Ws_MiscChargeSelected miscChargeSelected, boolean isSelected) {

    try {
        CarClassSettingDetails settingDetails = new CarClassSettingDetails(getActivity());

        if (miscChargeSelected.getCode() != null && !miscChargeSelected.getCode().equals("")) {
            settingDetails.setCode(miscChargeSelected.getCode());
        }

        if (miscChargeSelected.getName() != null && !miscChargeSelected.getName().equals("")) {
            settingDetails.setName(miscChargeSelected.getName());
        }

        if (miscChargeSelected.getIcon() != null && !miscChargeSelected.getIcon().equals("")) {
            Bitmap settingIcon = IconUtils.loadIcon(getActivity(), miscChargeSelected.getIcon());
            Bitmap settingIconSelected = IconUtils.loadIcon(getActivity(), miscChargeSelected.getIcon());
            settingDetails.setIcon(settingIcon);
            settingDetails.setIconSelected(settingIconSelected);
        }
        if (miscChargeSelected.getValue() != null && !miscChargeSelected.getValue().equals("")) {
            settingDetails.setValue(Ws_Value.fromInt(Integer.parseInt(miscChargeSelected.getValue())));
            settingDetails.setAmountBackgroundColor(Color.parseColor("#00ffffff"));
            settingDetails.setAmountTextColor(Color.parseColor("#ff00428e"));
        } else {
            settingDetails.setValue(null);
        }
        settingDetails.setViewBackgroundColor(Color.parseColor("#ffd4d4d4"));
        settingDetails.setBackgroundColorSelected(Color.parseColor("#ff616161"));

        if (isSelected) {
            //TODO: ADD TO TOP ADAPTER
            selectedSettingsList.add(settingDetails);
        } else {
            //TODO: ADD TO BOTTOM ADAPTER       
            unselectedSettingsList.add(settingDetails);
        }
    } catch (Exception e) {
        Log.e(TAG, e.getMessage());
    }
}

Misc Charge Details

public class MiscChargeSettingDetails {
    private Context context;
    private String code;
    private String name;
    private String description;
    private Bitmap icon = null;
    private Bitmap iconSelected = null;
    private Ws_Value value = null;
    private int amountBackgroundColor, amountTextColor, viewBackgroundColor, backgroundColorSelected;

    public MiscChargeSettingDetails(Context context) {
        this.context = context;
    }

    protected Context getContext() {
        return context;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Bitmap getIcon() {
        return icon;
    }

    public void setIcon(Bitmap icon) {
        this.icon = icon;
    }

    public Bitmap getIconSelected() {
        return iconSelected;
    }

    public void setIconSelected(Bitmap iconSelected) {
        this.iconSelected = iconSelected;
    }

    public Ws_Value getValue() {
        return value;
    }

    public void setValue(Ws_Value value) {
        this.value = value;
    }

    public int getAmountBackgroundColor() {
        return amountBackgroundColor;
    }

    public void setAmountBackgroundColor(int amountBackgroundColor) {
        this.amountBackgroundColor = amountBackgroundColor;
    }

    public int getAmountTextColor() {
        return amountTextColor;
    }

    public void setAmountTextColor(int amountTextColor) {
        this.amountTextColor = amountTextColor;
    }

    public int getViewBackgroundColor() {
        return viewBackgroundColor;
    }

    public void setViewBackgroundColor(int viewBackgroundColor) {
        this.viewBackgroundColor = viewBackgroundColor;
    }

    public int getBackgroundColorSelected() {
        return backgroundColorSelected;
    }

    public void setBackgroundColorSelected(int backgroundColorSelected) {
        this.backgroundColorSelected = backgroundColorSelected;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }
}
1

There are 1 best solutions below

6
On

Can you show the place where you're populating the list of SettingViews? Is your data model (SettingView) extends View? It is not the best idea to use the view object as model..