Reputation:
First of All Sorry for this Question title if it is not suitable for my case but am gonna explain my Case, First I created a fragment and created in this fragment a list View,and created another xml file which have the view for every item in list, created a java class for a setter and getter for this item.xml and created also an adapter class which have the view like this in this view there a small Relative layout with orange color which have the quantity and on the right of this small orange box I have a button which the function of this button should reduces the quantity of Item in list.So all I need if I Click on Item In List View it gonna be increase the quantity in this small box like for example if I click the pineapple item it will increse the number of item I clicked and replace instead of 0 will be 1 and id I Clicked another time on the same item it will replace instead of 1 by 2 and so on, for the small greay button it reduces the quantity in the small orange box this is the list View Fragment.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="abtech.waiteriano.com.waitrer.fragments.MenuLVFragment">
<ListView
android:id="@+id/menuLV"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
this is Fragment.Java
package abtech.waiteriano.com.waitrer.fragments;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import abtech.waiteriano.com.waitrer.MenuActivity;
import abtech.waiteriano.com.waitrer.R;
import abtech.waiteriano.com.waitrer.adapters.CustomMenuListViewAdapter;
import abtech.waiteriano.com.waitrer.connection_class.ConnectionClass;
import abtech.waiteriano.com.waitrer.getters_and_setters.MenuListItem;
public class MenuLVFragment extends android.app.Fragment {
View rootView;
ListView menuListView;
TextView TxtQty;
Button minusBtn;
static ArrayList<MenuListItem> listMenuArray = new ArrayList<MenuListItem>();
CustomMenuListViewAdapter customMenuListViewAdapter;
public MenuLVFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.fragment_menu_lv, container, false);
menuListView = (ListView) rootView.findViewById(R.id.menuLV);
customMenuListViewAdapter = new CustomMenuListViewAdapter(getActivity(), R.layout.menu_row_list, listMenuArray);
menuListView.setAdapter(customMenuListViewAdapter);
listMenuArray.clear();
String menuListSTR = "";
if (MenuActivity.Prst_ID.trim() == "-1")
menuListSTR = "Select ID,Code,Name,Name2 From Presets Where Active = 1 And Rest_ID_Active = 1 AND OutLet_ID_Active = 1 ORDER BY Code";
else
menuListSTR = "select dbo.MenuItems.Item_ID, dbo.Items.Code, dbo.Items.Name, dbo.Items.Name2, dbo.Items.PrintOnChick, dbo.Items.Taxable, dbo.Items.NoServiceCharge, dbo.Items.PrintOnReport,Case { fn IFNULL ((SELECT Price_Value FROM dbo.ItemsPrices WHERE (PriceLVL_ID = 1) AND (Item_ID = dbo.Items.ID)), 0) } when 0 then dbo.Items.StaticPrice Else { fn IFNULL ((SELECT Price_Value FROM dbo.ItemsPrices WHERE (PriceLVL_ID = 1) AND (Item_ID = dbo.Items.ID)), dbo.Items.StaticPrice) } END AS Price From dbo.MenuItems LEFT OUTER JOIN dbo.Items ON dbo.MenuItems.Item_ID = dbo.Items.ID Where (dbo.MenuItems.Preset_ID = " + MenuActivity.Prst_ID + ") AND (dbo.MenuItems.Rest_ID_Active = " + ConnectionClass.Rest_ID + ") AND (dbo.MenuItems.OutLet_ID_Active = " + ConnectionClass.OutletID + ") AND (dbo.Items.Active = 1) ORDER BY dbo.MenuItems.SortNumber";
ResultSet rs = ConnectionClass.Ret_RS(menuListSTR);
try {
while (rs.next()) {
listMenuArray.add(new MenuListItem(rs.getString("Name")));
}
} catch (SQLException e) {
e.printStackTrace();
}
menuListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(),"You Clicked Here",Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
}
this is item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:background="#bebdbd"
android:layout_height="80dp"
android:id="@+id/menuRL"
android:layout_width="fill_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/questionsign"
android:layout_marginStart="20dp"
android:id="@+id/itemImage"
android:layout_centerVertical="true"
android:layout_alignParentStart="true" />
<RelativeLayout
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginEnd="37dp"
android:id="@+id/qtyID"
android:background="#f9762f"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true">
<TextView
android:text="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtQTY"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textStyle="bold"
android:id="@+id/menulistTV2"
android:layout_alignTop="@+id/itemImage"
android:layout_toEndOf="@+id/itemImage" />
<Button
android:text="-"
android:textSize="15dp"
android:layout_width="35dp"
android:layout_height="35dp"
android:id="@+id/minusBtn"
android:layout_alignTop="@+id/menulistTV2"
android:layout_alignParentEnd="true" />
</RelativeLayout>
</LinearLayout>
and this is getter and setter class package abtech.waiteriano.com.waitrer.getters_and_setters;
import android.graphics.Bitmap;
/**
* Created by dell on 3/1/2017.
*/
public class MenuListItem {
Bitmap image;
String menuItemName;
String minusButton;
String plusButton;
String qtyView;
public MenuListItem(String menuItemName) {
super();
this.image = image;
this.menuItemName = menuItemName;
this.minusButton = minusButton;
this.plusButton = plusButton;
this.qtyView = qtyView;
}
public Bitmap getImage() {
return image;
}
public void setImage(Bitmap image) {
this.image = image;
}
public String getMenuItemName() {
return menuItemName;
}
public void setMenuItemName(String menuItemName) {
this.menuItemName = menuItemName;
}
public String getMinusButton() {
return minusButton;
}
public void setMinusButton(String minusButton) {
this.minusButton = minusButton;
}
public String getPlusButton() {
return plusButton;
}
public void setPlusButton(String plusButton) {
this.plusButton = plusButton;
}
public String getQtyView() {
return qtyView;
}
public void setQtyView(String qtyView) {
this.qtyView = qtyView;
}
}
and this is my Adapter Class
package abtech.waiteriano.com.waitrer.adapters;
import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.ArrayList;
import abtech.waiteriano.com.waitrer.R;
import abtech.waiteriano.com.waitrer.getters_and_setters.MenuListItem;
/**
* Created by dell on 3/1/2017.
*/
public class CustomMenuListViewAdapter extends ArrayAdapter<MenuListItem> {
Context context;
int layoutResourceId;
ArrayList<MenuListItem> dataListMenu = new ArrayList<MenuListItem>();
public CustomMenuListViewAdapter(Context context, int layoutResourceId, ArrayList<MenuListItem>dataListMenu) {
super(context,layoutResourceId,dataListMenu);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.dataListMenu = dataListMenu;
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listMenuRow = convertView;
RecordListMenuHolder recordListMenuHolder = null;
if(listMenuRow == null){
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
listMenuRow = inflater.inflate(layoutResourceId, parent, false);
recordListMenuHolder = new RecordListMenuHolder();
// recordListMenuHolder.menuItemImage = (String) listMenuRow.findViewById(R.id.itemImage);
recordListMenuHolder.listMenuTV = (TextView) listMenuRow.findViewById(R.id.menulistTV2);
recordListMenuHolder.qtyView = (RelativeLayout) listMenuRow.findViewById(R.id.qtyID);
recordListMenuHolder.minusBtn = (Button) listMenuRow.findViewById(R.id.minusBtn);
listMenuRow.setTag(recordListMenuHolder);
}else{
recordListMenuHolder = (RecordListMenuHolder)listMenuRow.getTag();
}
MenuListItem menuListItem = dataListMenu.get(position);
recordListMenuHolder.listMenuTV.setText(menuListItem.getMenuItemName());
return listMenuRow;
}
static class RecordListMenuHolder {
// Image menuItemImage;
TextView listMenuTV;
RelativeLayout qtyView;
Button minusBtn;
Button plusBtn;
}
}
sorry If My explanation is not understandable,if there any enquiry for codes please leave a comment
Upvotes: 1
Views: 55
Reputation: 493
public class CustomMenuListViewAdapter extends ArrayAdapter<MenuListItem> {
Context context;
int layoutResourceId;
ArrayList<MenuListItem> dataListMenu = new ArrayList<MenuListItem>();
RecordListMenuHolder recordListMenuHolder;
int qty = 0;
public CustomMenuListViewAdapter(Context context, int layoutResourceId, ArrayList<MenuListItem>dataListMenu) {
super(context,layoutResourceId,dataListMenu);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.dataListMenu = dataListMenu;
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listMenuRow = convertView;
if(listMenuRow == null){
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
listMenuRow = inflater.inflate(layoutResourceId, parent, false);
recordListMenuHolder = new RecordListMenuHolder();
// recordListMenuHolder.menuItemImage = (String) listMenuRow.findViewById(R.id.itemImage);
recordListMenuHolder.listMenuTV = (TextView) listMenuRow.findViewById(R.id.menulistTV2);
recordListMenuHolder.qtyView = (RelativeLayout) listMenuRow.findViewById(R.id.qtyID);
recordListMenuHolder.minusBtn = (Button) listMenuRow.findViewById(R.id.minusBtn);
listMenuRow.setTag(recordListMenuHolder);
}else{
recordListMenuHolder = (RecordListMenuHolder)listMenuRow.getTag();
}
MenuListItem menuListItem = dataListMenu.get(position);
recordListMenuHolder.listMenuTV.setText(menuListItem.getMenuItemName());
listMenuRow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
qty++;
recordListMenuHolder.qtyView.setText(""+qty);
}
});
return listMenuRow;
}
static class RecordListMenuHolder {
// Image menuItemImage;
TextView listMenuTV;
RelativeLayout qtyView;
Button minusBtn;
Button plusBtn;
}
}
Upvotes: 1