Android 如何在多微调器视图中获取项目数并计算总价
我正在列表视图(多个微调器)中为不同的产品实现微调器,左边是它们的图像,右边是价格。用户可以选择每个产品的数量 这项工作将在一个根据我的需要从BaseAdapter扩展而来的类中进行。 在微调器的getView中,我设置了微调器视图 现在我想: 1)当用户在微调器中选择一个项目时,该项目的价格计算为总价,右侧的文本视图文本设置为该总价。现在它工作正常,但是当我向上滚动列表时,微调器将其值更改为旧值(即位置0处的值),而不是新值 2)我想做的另一件事是将来自不同微调器的所有这些值保留在一个数组中,以便最终将所有不同微调器的值进一步计算为总和(起初,我在计算单个产品的价值,比如说该产品的价格是50美元,用户选择他想要20件该产品,所以总计=20x50) 3)另外一件事是获取一个微调器中所选项目的数量。以同样的方式将每个微调器的这些数字保留在另一个数组中,这样在最后,所有这些都被计算为所有产品的总数 下面是图片,很抱歉我的问题太长了,但我真的很想解决这个问题。如果你想从我这里得到更多的东西,请告诉我 当我选择项目时Android 如何在多微调器视图中获取项目数并计算总价,android,android-listview,android-spinner,Android,Android Listview,Android Spinner,我正在列表视图(多个微调器)中为不同的产品实现微调器,左边是它们的图像,右边是价格。用户可以选择每个产品的数量 这项工作将在一个根据我的需要从BaseAdapter扩展而来的类中进行。 在微调器的getView中,我设置了微调器视图 现在我想: 1)当用户在微调器中选择一个项目时,该项目的价格计算为总价,右侧的文本视图文本设置为该总价。现在它工作正常,但是当我向上滚动列表时,微调器将其值更改为旧值(即位置0处的值),而不是新值 2)我想做的另一件事是将来自不同微调器的所有这些值保留在一个数组中,
@SuppressWarnings("serial")
public class IDetails implements Serializable
{
ContentValues colmnValues;
private int no_of_items;
public float Totalprice;
public IDetails(ContentValues values )
{
colmnValues = values;
}
public String title() {
return getValue(colmnValues.get("title"));
}
public void setNo_of_items(int no_of_items) {
this.no_of_items = no_of_items;
}
public int getNo_of_items() {
return no_of_items;
}
public void setTotalprice(float Totalprice) {
this.Totalprice = Totalprice;
}
public float getTotalprice() {
return Totalprice;
}
public String imageUrl() {
return getValue(colmnValues.get("imageUrl"));
}
public String pprice() {
return getValue(colmnValues.get("Realprice"));
}
public String stock() {
return getValue(colmnValues.get("stock"));
}
private String getValue(Object obj){
if(obj == null){
return "";
}
return (String) obj;
}
}
当我滚动屏幕时,微调器中的所有值和文本视图中的价格将重置为初始位置
这是我的代码
public class Base_Adapter extends BaseAdapter
{
ImageView image;
TextView name, price;
Context context ;
ArrayList<ItemDetails> IDetails; //The item class which have methods and fields
RelativeLayout R_Layout;
Activity activit;
public Base_Adapter(Context context , ArrayList<ItemDetails> li)
{
this.context = context;
IDetails = li;
}
public void setLayout(Activity activity, RelativeLayout layout){
R_Layout = layout;
this.activit = activity;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return IDetails.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
//////// Get View For Spinner////
@Override
public View getView(final int position, View CV, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater infleter = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(CV == null)
{
CV = infleter.inflate(R.layout.base_adapter, null);
}
final ItemDetails item = IDetails.get(position);
int min =1;
int max = Integer.parseInt(item.totall_Available());
ArrayList<String> A_list= new ArrayList<String>();
for(int i=1;i<=max;i++)
{
A_list.add("Number of Items :"+i);
}
image = (ImageView) CV.findViewById(R.id.Item_image);
name = (TextView) CV.findViewById(R.id.item_name);
price = (TextView) CV.findViewById(R.id.item_price);
final Spinner quantity = (Spinner) CV.findViewById(R.id.items);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, R.layout.spinner_textview, A_list);
quantity.setAdapter(adapter);
//String selectedItem = (String) quantity.getSelectedItem();
name.setText(item.name());
/// ItemClick/////
quantity.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0, View arg1, int i, long arg3)
{
if(i>0){
float cal=Float.parseFloat(item.Fisrtprise());
float cal3=cal*i;
price.setText(""+String.format("%.2f", cal3).replace(".", ","));
String s = Float.toString(cal3);
item.Totalprice=s;
}
else{
price.setText(""+String.format("%.2f", Float.parseFloat(item.Fisrtprise())).replace(".", ","));
}
}
public void onNothingSelected(AdapterView<?> arg0){
}
});
return CV;
}
你需要像徽章(选定数量)*mrp这样的东西,它将给出总金额 供参考
振作起来,这个答案太长了 您已经有了一个类,
IDetails
,它描述了待售商品。我会建议一种稍微不同的方法,但如果您不喜欢,请跳到适配器部分。不过,我会在整个回答过程中使用我修改过的模型,因为它们使你的一些问题变得更容易
模型
@SuppressWarnings("serial")
public class IDetails implements Serializable
{
ContentValues colmnValues;
private int no_of_items;
public float Totalprice;
public IDetails(ContentValues values )
{
colmnValues = values;
}
public String title() {
return getValue(colmnValues.get("title"));
}
public void setNo_of_items(int no_of_items) {
this.no_of_items = no_of_items;
}
public int getNo_of_items() {
return no_of_items;
}
public void setTotalprice(float Totalprice) {
this.Totalprice = Totalprice;
}
public float getTotalprice() {
return Totalprice;
}
public String imageUrl() {
return getValue(colmnValues.get("imageUrl"));
}
public String pprice() {
return getValue(colmnValues.get("Realprice"));
}
public String stock() {
return getValue(colmnValues.get("stock"));
}
private String getValue(Object obj){
if(obj == null){
return "";
}
return (String) obj;
}
}
创建描述项的基类。我们将使这个类抽象,并对其进行扩展,以描述您销售的单个商品
public abstract class ShoppingItem {
/* You can add more fields here, for example an image */
protected String title;
protected String description;
protected int cost;
// Each object keeps track of how many of itself is being bought
protected int numberBought = 1;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getCost() {
return cost;
}
public void setCost(int cost) {
this.cost = cost;
}
public int getNumberBought() {
return numberBought;
}
public void setNumberBought(int numberBought) {
this.numberBought = numberBought;
}
}
现在可以轻松创建单个项目:
public class Banana extends ShoppingItem {
public Banana() {
this.title = "Banana";
this.description = "A tasty banana";
this.cost = 100;
}
}
public class Apple extends ShoppingItem {
public Apple() {
this.title = "Apple";
this.description = "A fresh apple";
this.cost = 200;
}
}
适配器 现在我们已经把所有的模型都整理好了,我们如何用它们以最有效的方式填充一个
ListView
?使用自定义的适配器
,就像您所做的那样。我们将修复滚动时微调器重置的问题,并且还将添加一些漂亮的性能增强
public class ShoppingAdapter extends ArrayAdapter<ShoppingItem> {
// We use this LayoutInflater to inflate the xml for each row in the list
private LayoutInflater mInflater;
// This is the strings that will populate the spinners
// Can be moved to res/values/string-array.xml to prevent hardcoded strings
private String[] spinnerArray = new String[] { "No. items: 1",
"No. items: 2",
"No. items: 3",
"No. items: 4"};
public ShoppingAdapter(Context context, int resource, ArrayList<ShoppingItem> objects) {
super(context, 0, objects);
// Initialize the declared LayoutInflater
mInflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// This is a simple reference holding class, so we don't have to call
// findViewById() every time getView() is called. Improved performance :)
ViewHolder holder = null;
if(convertView == null) {
// If convertView is null, then we don't have a view to place
// our data in. We have to inflate one ourself
convertView = mInflater.inflate(R.layout.list_item, null);
// Store a reference to the Spinner and the TextView in
// the ViewHolder object
holder = new ViewHolder();
holder.spinner = (Spinner) convertView.findViewById(R.id.spinner);
holder.textView = (TextView) convertView.findViewById(R.id.total);
// Attach the ViewHolder object to convertView, as a tag
convertView.setTag(holder);
}
else {
// If convertView is non-null then we can fetch the ViewHolder object
// we stored in the tag earlier. We now have references to both the Spinner
// and TextView, through the ViewHolder object.
holder = (ViewHolder) convertView.getTag();
}
// If the holder is non-null, we are good to go!
if(holder != null) {
// Fetch the item from the underlying ArrayList
final ShoppingItem item = getItem(position);
// We need this to be final, so we can access it inside the
// onItemSelected()-callback
final TextView textview = holder.textView;
// Populate the Spinner
holder.spinner.setAdapter(new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item, spinnerArray));
// Add a callback when an item is selected in the Spinner
holder.spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View v, int pos, long id) {
// Spinner position is 0-based, so we have to increment
// by one
item.setNumberBought(pos + 1);
// Update the total amount in the TextView
textview.setText(Integer.toString(item.getCost() * item.getNumberBought()));
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {} // We don't care
});
// Set the spinner to the correct position
// By asking the underlying item how many is bought,
// the spinner will always show the correct number.
holder.spinner.setSelection(getItem(position).getNumberBought()-1);
}
return convertView;
}
// This guys job is to hold references to the
// views we need to access in each row
private static class ViewHolder {
public Spinner spinner;
public TextView textView;
}
}
我只是更新了你的代码,没有创建新的。首先,我将回答这三个问题
getView()
方法,它们将再次初始化为默认值。
要避免这种情况并恢复以前选择的值,请执行以下操作:
我们要做的就是将它们保存在地图中,然后重新分配。接下来
代码“selectedItem
”是我用来存储所选金额的地图
对于每个微调器totalPrices
”用于它。在onItemSelected()中,我们可以将总数
特定微调器的数量。在“BUY
”的onclick()方法中
按钮您可以通过调用getTotalPrice()
方法访问此映射
是适配器类
selectedItem
”地图存储
微调器的选定值。与总价相同,您可以获得
此映射使用getTotalItems()
onItemSelected()
方法内的价格计算中,您使用“i
”乘以项目价格。它应该是(i+1)
。因为“i”是列表在微调器中的位置。它从0
开始
这是基本的_适配器类
public class Base_Adapter extends BaseAdapter{
ImageView image;
TextView name, price;
private HashMap<Integer,Integer> selectedItem=new HashMap<Integer, Integer>();
private HashMap<Integer, String> totalPrices=new HashMap<Integer, String>();
Context context ;
ArrayList<ItemDetails> IDetails; //The item class which have methods and fields
RelativeLayout R_Layout;
Activity activit;
public Base_Adapter(Context context , ArrayList<ItemDetails> li) {
// TODO Auto-generated constructor stub
this.context = context;
IDetails = li;
}
public void setLayout(Activity activity, RelativeLayout layout){
R_Layout = layout;
this.activit = activity;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return IDetails.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public HashMap<Integer,Integer> getTotalItems(){
return selectedItem;
}
public HashMap<Integer, String> getTotalPrice(){
return totalPrices;
}
//////// Get View For Spinner////
@Override
public View getView(final int position, View CV, ViewGroup parent) {
// TODO Auto-generated method stub
final ItemDetails item = IDetails.get(position);
int min =1;
int max = Integer.parseInt(item.stock());
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView=inflater.inflate(R.layout.base_adapter, parent,false);
ArrayList<String> A_list= new ArrayList<String>();
for(int i=1;i<=max;i++)
{
A_list.add("Number of Items :"+i);
}
ImageView image=(ImageView) rowView.findViewById(R.id.item_image);
TextView nameTextView=(TextView) rowView.findViewById(R.id.item_name);
nameTextView.setText(item.title());
final TextView price=(TextView) rowView.findViewById(R.id.item_price);
Spinner quantity=(Spinner) rowView.findViewById(R.id.items);
ArrayAdapter<String > quatity=new ArrayAdapter<String>(context, R.layout.spinner_textview, R.id.item_list, A_list);
quantity.setAdapter(quatity);
if(selectedItem.get(position) != null){
//This should call after setAdapter
quantity.setSelection(selectedItem.get(position));
}
quantity.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0, View arg1, int i, long arg3)
{
if(i>0){
selectedItem.put(position, i);
i=i+1;
float cal=Float.parseFloat(item.price());
float cal3=cal*i;
price.setText(""+String.format("%.2f", cal3).replace(".", ","));
item.Totalprice= cal3;
}
else{
price.setText(""+String.format("%.2f", Float.parseFloat(item.price())).replace(".", ","));
}
totalPrices.put(position, price.getText().toString());
}
public void onNothingSelected(AdapterView<?> arg0){
}
});
return rowView;
}
}
Spinners
正在重置,因为当您选择一个新数量时,您没有将所选内容保存在ItemDetails
类中(在该类中,您有一个项目数量字段(在getView()
方法中,您有ItemDetails
和下面的IDetails
,它们是相同的吗?!))。保存ItemDetails
对象中的项数后,您可以在getView方法中使用该值,并将微调器上的选择设置为该值(使用setSelection(/*property value*/)
),很抱歉,构造函数名称中的键入错误。这更像是一个编程练习,这不是一个编程问题。您卡在哪里了?您需要将用户选择的微调器位置和textview计算值保留在您的数据域(Datalist对象)中。所以,当你们向上滚动时,从数据列表中检查旧值并相应地更新。非常感谢你们的努力。我自己解决了这个问题,现在我的代码比上面提到的要先进得多。你在这个问题上花了很多时间,我很高兴你的帮助,再次感谢你。你能在这里发布你的解决方案吗@安卓男
public class Base_Adapter extends BaseAdapter{
ImageView image;
TextView name, price;
private HashMap<Integer,Integer> selectedItem=new HashMap<Integer, Integer>();
private HashMap<Integer, String> totalPrices=new HashMap<Integer, String>();
Context context ;
ArrayList<ItemDetails> IDetails; //The item class which have methods and fields
RelativeLayout R_Layout;
Activity activit;
public Base_Adapter(Context context , ArrayList<ItemDetails> li) {
// TODO Auto-generated constructor stub
this.context = context;
IDetails = li;
}
public void setLayout(Activity activity, RelativeLayout layout){
R_Layout = layout;
this.activit = activity;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return IDetails.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public HashMap<Integer,Integer> getTotalItems(){
return selectedItem;
}
public HashMap<Integer, String> getTotalPrice(){
return totalPrices;
}
//////// Get View For Spinner////
@Override
public View getView(final int position, View CV, ViewGroup parent) {
// TODO Auto-generated method stub
final ItemDetails item = IDetails.get(position);
int min =1;
int max = Integer.parseInt(item.stock());
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView=inflater.inflate(R.layout.base_adapter, parent,false);
ArrayList<String> A_list= new ArrayList<String>();
for(int i=1;i<=max;i++)
{
A_list.add("Number of Items :"+i);
}
ImageView image=(ImageView) rowView.findViewById(R.id.item_image);
TextView nameTextView=(TextView) rowView.findViewById(R.id.item_name);
nameTextView.setText(item.title());
final TextView price=(TextView) rowView.findViewById(R.id.item_price);
Spinner quantity=(Spinner) rowView.findViewById(R.id.items);
ArrayAdapter<String > quatity=new ArrayAdapter<String>(context, R.layout.spinner_textview, R.id.item_list, A_list);
quantity.setAdapter(quatity);
if(selectedItem.get(position) != null){
//This should call after setAdapter
quantity.setSelection(selectedItem.get(position));
}
quantity.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0, View arg1, int i, long arg3)
{
if(i>0){
selectedItem.put(position, i);
i=i+1;
float cal=Float.parseFloat(item.price());
float cal3=cal*i;
price.setText(""+String.format("%.2f", cal3).replace(".", ","));
item.Totalprice= cal3;
}
else{
price.setText(""+String.format("%.2f", Float.parseFloat(item.price())).replace(".", ","));
}
totalPrices.put(position, price.getText().toString());
}
public void onNothingSelected(AdapterView<?> arg0){
}
});
return rowView;
}
}
public void getTotal(View view){
HashMap<Integer, Integer>totalItems=adapter.getTotalItems();
HashMap<Integer, String> totalPrices=adapter.getTotalPrice();
for(int i=0;i<totalItems.size();i++){
if(totalItems.get(i) != null){
System.out.println("Spinner No"+(i+1)+"Items :"+(totalItems.get(i)+1));
System.out.println("Spinner No "+(i+1)+"total price "+totalPrices.get(i));
}else{
System.out.println("Spinner No"+(i+1)+"Items : 1");
}
}