如何在ListView中显示结果特定项目

时间:2014-04-11 18:36:08

标签: java android

我想要Listview中食物的总计算价格。

如果您选择多个食物迭代并按下“订购”按钮。

您获得结果总价。

如何显示我的总价格请帮助我。

我是android的初学者。请帮帮我

MenuListItem:

package com.example.resturetnordering;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.ClipData.Item;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class MenuListItem extends Activity{


    ListView lv;
    MyAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    setContentView(R.layout.list_layout);

    lv=(ListView)findViewById(R.id.listView1);

    adapter=new MyAdapter(this , getListItem());
    lv.setAdapter(adapter);

    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View arg1, int position,
                long arg3) {
            // TODO Auto-generated method stub

//          String ls= lv.getItemAtPosition(position).toString();
//          Toast.makeText(getApplicationContext(), String.valueOf(ls) , Toast.LENGTH_LONG).show();
//            
            adapter.toggleChecked(position);

        }


    });




    }


    public void order(View v) {

        String result="";
        Integer a;
        int r;

        ArrayList<ListItem> res=getListItem();
        List<Integer> resultList=adapter.getCheckedItemPosition();
        for (int i = 0; i < resultList.size(); i++) {
            result+=String.valueOf(resultList.get(i))+"\n"; 


        }
        Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
        Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();




    }


    public ArrayList<ListItem> getListItem() {
        ArrayList<ListItem> alllist=new ArrayList<ListItem>();

        ListItem l1=new ListItem("beef_burguer", 1, R.drawable.beef_burguer);
        ListItem l2=new ListItem("bacon_cheese_burger", 2, R.drawable.bacon_cheese_burger);
        ListItem l3=new ListItem("frings", 3, R.drawable.frings);
        ListItem l4=new ListItem("burger_kingse", 4, R.drawable.burger_kingse);
        ListItem l5=new ListItem("mint_oreo", 5, R.drawable.mint_oreo);
        ListItem l6=new ListItem("sourdough_bread", 6, R.drawable.sourdough_bread);
        ListItem l7=new ListItem("sandwich", 7, R.drawable.sandwich);
        ListItem l8=new ListItem("sandwich_loaded", 8, R.drawable.sandwich_loaded);
        ListItem l9=new ListItem("mustard", 9, R.drawable.mustard);
        ListItem l10=new ListItem("cheese_burger", 10, R.drawable.cheese_burger);

        alllist.add(l1);
        alllist.add(l2);
        alllist.add(l3);
        alllist.add(l4);
        alllist.add(l5);
        alllist.add(l6);
        alllist.add(l7);
        alllist.add(l8);
        alllist.add(l9);
        alllist.add(l10);

        return alllist;
    }





}

列表项:

package com.example.resturetnordering;

public class ListItem {

    String name;
    Integer price;
    Integer image;



    public ListItem(String name, Integer price, Integer image) {
        super();
        this.name = name;
        this.price = price;
        this.image = image;
    }


    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getPrice() {
        return price;
    }
    public void setPrice(Integer price) {
        this.price = price;
    }
    public Integer getImage() {
        return image;
    }
    public void setImage(Integer image) {
        image = image;
    }
    @Override
    public String toString() {
        return "ListItem [name=" + name + ", price=" + price + ", Image="
                + image + "]";
    }



}

MyAdapter:

package com.example.resturetnordering;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MyAdapter extends ArrayAdapter<ListItem>{

     Activity context;
        ArrayList<ListItem> listItem;

        HashMap<Integer, Boolean> myChecked=new HashMap<Integer, Boolean>();

        public MyAdapter(Activity context, ArrayList<ListItem> listItem) {
            super(context, R.layout.adaptermy_layout, listItem);
             this.context=context;
             this.listItem=listItem;

           for (int i = 0; i < listItem.size(); i++) {
            myChecked.put(i, false);
        }

        }

        public void toggleChecked(int position) {
            if(myChecked.get(position)){
                myChecked.put(position, false);
                Toast.makeText(getContext(), "false", Toast.LENGTH_LONG).show();
            }else{
                myChecked.put(position, true);
                Toast.makeText(getContext(), "true", Toast.LENGTH_LONG).show();
            }
            notifyDataSetChanged();
        }

        public List<Integer> getCheckedItemPosition() {
            List<Integer> checkedItemPosition=new ArrayList<Integer>();

            for (int i = 0; i < myChecked.size(); i++) {
                if(myChecked.get(i)){
                     checkedItemPosition.add(i);
                }

            }
            return checkedItemPosition;
        }

    //  public List<Integer> getCheckedItem() {
//        List<Integer> checkedItem=new ArrayList<Integer>();
//        
//        for (int i = 0; i < myChecked.size(); i++) {
//            checkedItem.add();
//        }
//        return checkedItem;
    //  }




        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view=convertView;

                LayoutInflater inflat=context.getLayoutInflater();
                view=inflat.inflate(R.layout.adaptermy_layout, null, false);
                TextView txtName=(TextView) view.findViewById(R.id.txtName);
                TextView txtPrice=(TextView) view.findViewById(R.id.txtPrice);
                ImageView image=(ImageView) view.findViewById(R.id.imageView1);

                ListItem l=listItem.get(position);

                txtName.setText(l.getName());
                txtPrice.setText(String.valueOf(l.getPrice()));
                image.setImageResource(l.getImage());

                CheckBox chekboxs=(CheckBox) view.findViewById(R.id.checkBox1);

                Boolean chekBox=myChecked.get(position);

                if(chekBox!=null){
                    chekboxs.setChecked(chekBox);
                }


            return view;
        }




}

1 个答案:

答案 0 :(得分:1)

您必须在价格上执行整数加法(不是字符串)。像:

public void order(View v) {

    String result="";
    Integer a=0;
    int r=0; // You can use either 'a' or 'r' for addition.

    ArrayList<ListItem> res=getListItem();
    List<Integer> resultList=adapter.getCheckedItemPosition();
    for (int i = 0; i < resultList.size(); i++) {
        a+=res.get(resultList.get(i)).getPrice(); 
    }
    result="Total:"+a;
    Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();

}