我将最少十种食物送入Listview。
我想选择一种或多种食物。
按下订单按钮。
然后显示精选食品的总价格。
请告诉我怎么可能?
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<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;
}
public void order(View v) {
String result="";
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();
}
}
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<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.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<Integer> getCheckedItemPosition() {
List<Integer> checkedItemPosition=new ArrayList<Integer>();
for (int i = 0; i < myChecked.size(); 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.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;
}
}
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 + "]";
}
}
我使用它但它不起作用:
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 :(得分:1)
您只需在列表视图中设置android:choiceMode="multipleChoice"
即可。您还可以实现选择器以定义如何突出显示所选项目。选择器的一个例子:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/list_item_bg_normal" android:state_activated="false"/>
<item android:drawable="@drawable/list_item_bg_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/list_item_bg_pressed" android:state_activated="true"/>
</selector>
选择ListView
中的一个或多个项目后,您可以通过以下方式获取所选项目:
int len = listView.getCount();
SparseBooleanArray checked = listView.getCheckedItemPositions();
for (int i = 0; i < len; i++)
if (checked.get(i)) {
String item = cont_list.get(i);
/* calculate the price */
}
答案 1 :(得分:0)
您可以使用自定义列表视图为列表视图中的每个项目添加复选框。在按钮点击事件上,您可以遍历列表以识别已选择的项目并计算总金额。