So, i have menu items being displayed in listview, along with checkboxes, for the user to select which he likes. And i have an overhead cart icon which keeps track of no of checked items, by a function that is written in the adapter view.
My problem:
When i have implemented the cart increment function inside of OnItemClick listener, and then when i check a few items,it doesn't get updated unless i make a click outside of checkbox (anywhere else in the listview). I have tried disabling descendant focusability and modifying focusability as per other SO answers. They haven't solved my issue.
My calling activity
adapter = new MenuAdapter(CreateOrder.this,itemslist); listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
selecteditems = adapter.getSelectedList();
Log.e("Selected Items",selecteditems.toString());
size = selecteditems.size();
qo.setText(String.valueOf(size));
Log.e("No of main items ",String.valueOf(size));
}
});
My Adapter class
public class MenuAdapter extends BaseAdapter {
Context mContext;
List<MainItem> linkedList;
protected LayoutInflater vi;
private boolean[] checkBoxState = null;
private int[] itemsqty;
private HashMap<MainItem, Boolean> checkedForItem = new HashMap<>();
public MenuAdapter(Context context, List<MainItem> linkedList) {
this.mContext = context;
this.linkedList = linkedList;
itemsqty = new int[linkedList.size()];
Arrays.fill(itemsqty,0);
this.vi = (LayoutInflater)context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return linkedList.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = vi.inflate(R.layout.row_layout, parent, false);
holder = new ViewHolder();
holder.iname = (TextView) convertView.findViewById(R.id.tvname);
holder.iprice = (TextView) convertView.findViewById(R.id.tvprice);
holder.selectionBox = (CheckBox) convertView.findViewById(R.id.checkBox);
holder.plus = (TextView)convertView.findViewById(R.id.tvplus);
holder.minus = (TextView)convertView.findViewById(R.id.tvminus);
holder.qty = (TextView)convertView.findViewById(R.id.tvqty);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
final MainItem item = linkedList.get(position);
checkBoxState = new boolean[linkedList.size()];
holder.iname.setText(item.getItemname());
holder.iprice.setText(item.getItemprice());
/** checkBoxState has the value of checkBox ie true or false,
* The position is used so that on scroll your selected checkBox maintain its state **/
if(checkBoxState != null)
holder.selectionBox.setChecked(checkBoxState[position]);
//new added 1
if(itemsqty!=null)
{
holder.qty.setText(String.valueOf(itemsqty[position]));
}
holder.selectionBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(((CheckBox)v).isChecked()) {
checkBoxState[position] = true;
ischecked(position,true);
}
else {
checkBoxState[position] = false;
ischecked(position,false);
}
}
});
holder.plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.e("Plus clicked","at"+position);
if(itemsqty[position]<=10)
itemsqty[position]+=1;
holder.qty.setText(String.valueOf(itemsqty[position]));
}
});
holder.minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.e("Minus clicked ","at"+position);
if(itemsqty[position]>0)
{
itemsqty[position]-=1;
holder.qty.setText(String.valueOf(itemsqty[position]));
}
}
});
if (checkedForItem.get(item) != null) {
holder.selectionBox.setChecked(checkedForItem.get(item));
}
/**Set tag to all checkBox**/
holder.selectionBox.setTag(item);
return convertView;
}
private class ViewHolder {
TextView iname;
TextView iprice;
CheckBox selectionBox;
TextView plus;
TextView minus;
TextView qty;
}
public void ischecked(int position,boolean flag )
{
checkedForItem.put(this.linkedList.get(position), flag);
}
public int[] getselectedquantity()
{
return itemsqty;
}
public LinkedList<MainItem> getSelectedList(){
LinkedList<MainItem> selectedlist = new LinkedList<>();
LinkedList<String> List = new LinkedList<>();
for (Map.Entry<MainItem, Boolean> pair : checkedForItem.entrySet()) {
if(pair.getValue()) {
//List.add(pair.getKey().getItemname());
for(int j=0;j<linkedList.size();j++)
{
if(linkedList.get(j).getItemname().equals(pair.getKey().getItemname()))
{
pair.getKey().setQuantity(itemsqty[j]);
Log.e("quantity ## ",String.valueOf(itemsqty[j]));
}
}
selectedlist.add(pair.getKey());
}
}
return selectedlist;
}
}
Aucun commentaire:
Enregistrer un commentaire