Reputation: 83
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?
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 + "]";
}
}
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();
}
});
Upvotes: 0
Views: 5098
Reputation: 2851
You can simply set the android:choiceMode="multipleChoice"
on your listview. You can also implement a selector to define how the selected item will be highlighted. An example of a selector:
<?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>
Once one or more items in the ListView
are selected, you can get the selected items by:
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 */
}
Upvotes: 1
Reputation: 188
You can use a custom listview to add checkboxes to each item in your listview. ANd on button click event you can traverse your list to identify which items have been selected and calculate the total amount.
Upvotes: 0