android listview selecteditems,Android Listview Select Item

I have minimum Ten item food into Listview.

I want select one or more item food .

And pressed Order button.

Then show me total price for select item food.

How possible please tell me?

MenuListItem:

package com.example.project_hotel_management;

import java.util.ArrayList;

import java.util.List;

import android.app.Activity;

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.menuitemlist_layout);

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

adapter=new MyAdapter(this , getListItem());

lv.setAdapter(adapter);

lv.setOnItemClickListener(new OnItemClickListener() {

@Override

public void onItemClick(AdapterView> arg0, View arg1, int arg2,

long arg3) {

// TODO Auto-generated method stub

Toast.makeText(getApplicationContext(), "d", Toast.LENGTH_LONG).show();

}

});

}

public ArrayList getListItem() {

ArrayList alllist=new ArrayList();

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;

}

public void order(View v) {

String result="";

List 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();

}

}

MyAdapter:

package com.example.project_hotel_management;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import android.app.Activity;

import android.content.ClipData.Item;

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{

Activity context;

ArrayList listItem;

HashMap myChecked=new HashMap();

public MyAdapter(Activity context, ArrayList listItem) {

super(context, R.layout.adapter_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(), "false", Toast.LENGTH_LONG).show();

}

notifyDataSetChanged();

}

public List getCheckedItemPosition() {

List checkedItemPosition=new ArrayList();

for (int i = 0; i < myChecked.size(); i++) {

checkedItemPosition.add(i);

}

return checkedItemPosition;

}

// public List getCheckedItem() {

// List checkedItem=new ArrayList();

//

// 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.adapter_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);

Toast.makeText(getContext(), String.valueOf(listItem.get(position)), Toast.LENGTH_LONG

).show();

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;

}

}

ListItem:

package com.example.project_hotel_management;

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 + "]";

}

}

I use this but it's not working:

lv.setOnItemClickListener(new OnItemClickListener() {

@Override

public void onItemClick(AdapterView> arg0, View arg1, int position,

long arg3) {

// TODO Auto-generated method stub

ListItem ls=getListItem().get(position);

Toast.makeText(getApplicationContext(), String.valueOf(ls.getPrice()), Toast.LENGTH_LONG).show();

}

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值