package com.example.listview_multiples_selection03;

import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
import android.content.Context;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
public class ListViewAdapter extends ArrayAdapter<Shopping> {
// Declare Variables
Context context;
LayoutInflater inflater;
List<Shopping> shoppinglist;
private SparseBooleanArray mSelectedItemsIds;
public ListViewAdapter(Context context, int resourceId,
List<Shopping> shoppinglist) {
super(context, resourceId, shoppinglist);
mSelectedItemsIds = new SparseBooleanArray();
this.context = context;
this.shoppinglist = shoppinglist;
inflater = LayoutInflater.from(context);
}
private class ViewHolder {
TextView Item;
TextView Price;
}
public View getView(int position, View view, ViewGroup parent) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.listview_item, null);
// Locate the TextViews in listview_item.xml
holder.Item = (TextView) view.findViewById(R.id.Item);
holder.Price = (TextView) view.findViewById(R.id.Price);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
// Capture position and set to the TextViews
holder.Item.setText(shoppinglist.get(position).getItem());
holder.Price.setText(shoppinglist.get(position).getPrice());
return view;
}
@Override
public void remove(Shopping object) {
shoppinglist.remove(object);
notifyDataSetChanged();
}
public List<Shopping> getShopping() {
return shoppinglist;

}
public void toggleSelection(int position) {
selectView(position, !mSelectedItemsIds.get(position));
}
public void removeSelection() {
mSelectedItemsIds = new SparseBooleanArray();
notifyDataSetChanged();
}
public void selectView(int position, boolean value) {
if (value)
mSelectedItemsIds.put(position, value);
else
mSelectedItemsIds.delete(position);
notifyDataSetChanged();
}
public int getSelectedCount() {
return mSelectedItemsIds.size();
}
public SparseBooleanArray getSelectedIds() {
return mSelectedItemsIds;
}
}