BreakfastService.java
public class BreakfastService extends Activity {
ListView ls;
ImageView image;
TextView code;
CheckBox name;
private ArrayList<Bfs> list;
MyCustomAdapter dataAdapter = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.breakfast_service);
list = Utility.getmDetails(this);
displayListView();
checkButtonClick();
}
private void displayListView() {
ArrayList<ItemVO> ItemList = new ArrayList<ItemVO>();
dataAdapter = new MyCustomAdapter(this, R.layout.breakfast_service, ItemList);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(dataAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
ItemVO item = (ItemVO) parent.getItemAtPosition(position);Toast.makeText(getApplicationContext(), "Clicked: " + Bfs.getCategoryName(),
Toast.LENGTH_LONG).show();
}
});
}
private class MyCustomAdapter extends ArrayAdapter<ItemVO> {
private ArrayList<ItemVO> itemList;
public MyCustomAdapter(Context context, int textViewResourceId,
ArrayList<ItemVO> itemList) {
super(context, textViewResourceId, itemList);
this. itemList = new ArrayList<ItemVO>();
this. itemList.addAll( itemList);
}
private class ViewHolder {
ImageView image;
TextView code;
CheckBox name;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Log.v("ConvertView", String.valueOf(position));
if (convertView == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.list, null);
holder = new ViewHolder();
holder.image=(ImageView) convertView.findViewById(R.id.imageViewi);
holder.code = (TextView) convertView.findViewById(R.id.textViewi);
holder.name = (CheckBox) convertView.findViewById(R.id.checkBox1);
image.setImageResource(list .get(position).getItemphoto());
code.setText(list .get(position).getItemdescription());
convertView.setTag(holder);
Log.d("TestTag", "Item Name : "+code );
holder.name.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v ;
ItemVO item = (ItemVO) cb.getTag();
Toast.makeText(getApplicationContext(), "Clicked: " + cb.getText() +" is " + cb.isChecked(),
Toast.LENGTH_LONG).show();
item.setSelected(cb.isChecked());
}
});
}
else {
holder = (ViewHolder) convertView.getTag();
}
ItemVO item = itemList.get(position);
holder.image.setImageResource(item.getItemphoto());
holder.code.setText(item.getItemdescription());
holder.name.setChecked(item.isSelected());
holder.name.setTag(item);
return convertView;
}
}
private void checkButtonClick() {
Button myButton = (Button) findViewById(R.id.button12);
myButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
StringBuffer responseText = new StringBuffer();
responseText.append("The following were selected...\n");
ArrayList<ItemVO> countryList = dataAdapter.itemList;
for(int i=0;i<countryList.size();i++){
ItemVO item = countryList.get(i);
if(item.isSelected()){
responseText.append("\n" + item.getItemdescription());
}
}
Toast.makeText(getApplicationContext(),
responseText, Toast.LENGTH_LONG).show();
}
});
}
}
Utility.java
package com.example.krishnaupahara;
import java.io.InputStream;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import beanclass.*;
import android.content.Context;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.ListView;
public class Utility
{
private static ArrayList<Bfs> mDetails;
public static void init(Context context)
{
getmDetails(context);
}
public static ArrayList<Bfs> getmDetails(Context context)
{
if(mDetails == null)
{
mDetails = new ArrayList<Bfs>();
try {
Document doc = parseXML(readFromFile("bs.xml",context));
NodeList node = doc.getElementsByTagName("Category");
for (int i=0; i<node.getLength(); i++)
{
Bfs mtype = new Bfs();
ArrayList<ItemVO> ServiceName = new ArrayList<ItemVO>();
Log.d("Test", "length"+node.getLength());
Element e = (Element) node.item(i);
Element categoryNameE = (Element) e.getElementsByTagName("CategoryName").item(0);
Element categoryCodeE = (Element) e.getElementsByTagName("CategoryCode").item(0);
String categoryNameS = categoryNameE.getLastChild().getNodeValue();
String categoryCodeS = categoryCodeE.getLastChild().getNodeValue();
mtype.setCategoryName(categoryNameS);
mtype.setCategoryCode(categoryCodeS);
Log.d("TestTag", "MerchantTypeName from XML is :" +categoryNameS +""+categoryCodeS );
mtype.setCategoryName(categoryNameS);
NodeList node2 = e.getElementsByTagName("Item");
for (int k=0; k<node2.getLength();k++)
{
ItemVO ItemS = new ItemVO();
Log.d("Test", "length22"+node2.getLength());
Element mItemEl = (Element) node2.item(k);
Element itemCodeEl = (Element) mItemEl.getElementsByTagName("Itemcode").item(0);
Element itemDescriptionEl = (Element) mItemEl.getElementsByTagName("Itemdesription").item(0);
Element itemPhotoEl = (Element) mItemEl.getElementsByTagName("Itemphoto").item(0);
ItemS.setItemdescription(itemDescriptionEl.getLastChild().getNodeValue());
ItemS.setItemphoto((Integer.valueOf(itemPhotoEl.getLastChild().getNodeValue())));
ItemS.setCategoryName(categoryNameS);
ItemS.setItemcode(itemCodeEl.getLastChild().getNodeValue());
ServiceName.add(ItemS);
}
mtype.setGetItemDetails(ServiceName);
mDetails.add(mtype);
Log.d("TestTag", "Values in arraylist in XML"+mDetails.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
}
return mDetails;
}
public static Document parseXML(InputSource source) throws Exception
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db;
Document doc = null;
try {
db = dbf.newDocumentBuilder();
doc = db.parse(source);
} catch (Exception e) {
throw e;
}
return doc;
}
public static InputSource readFromFile(String fileName, Context context)
{
InputSource inputSource = null;
InputStream fIn = null;
try {
fIn = context.getResources().getAssets().open(fileName);
inputSource = new InputSource(fIn);
} catch (Exception e) {
e.printStackTrace();
}
return inputSource;
}
}
Bfs.java Bean file
package beanclass;
import com.example.krishnaupahara.*;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class Bfs extends Activity {
private static String CategoryName;
private String CategoryCode;
private String Itemcode;
private String Itemdescription;
private int Itemphoto;
boolean selected = false;
private ArrayList<ItemVO> GetItemDetails;
public ArrayList<ItemVO> getGetItemDetails() {
return GetItemDetails;
}
public void setGetItemDetails(ArrayList<ItemVO> getItemDetails) {
GetItemDetails = getItemDetails;
}
public static String getCategoryName() {
return CategoryName;
}
public void setCategoryName(String categoryName) {
CategoryName = categoryName;
}
public String getCategoryCode() {
return CategoryCode;
}
public void setCategoryCode(String categoryCode) {
CategoryCode = categoryCode;
}
public String getItemcode() {
return Itemcode;
}
public void setItemcode(String itemcode) {
Itemcode = itemcode;
}
public String getItemdescription() {
return Itemdescription;
}
public void setItemdescription(String itemdescription) {
Itemdescription = itemdescription;
}
public int getItemphoto()
{
return Itemphoto;
}
public void setItemphoto(int Itemphoto)
{
this. Itemphoto = Itemphoto;
}
public void setEnabled(boolean b) {
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
}
ItemVo.java
package beanclass;
public class ItemVO
{
private String CategoryName;
private String CategoryCode;
private String Itemcode;
private String Itemdescription;
private int Itemphoto;
boolean selected = false;
public String getCategoryCode() {
return CategoryCode;
}
public void setCategoryCode(String categoryCode) {
CategoryCode = categoryCode;
}
public String getCategoryName() {
return CategoryName;
}
public void setCategoryName(String categoryName) {
CategoryName = categoryName;
}
public String getItemcode() {
return Itemcode;
}
public void setItemcode(String itemcode) {
Itemcode = itemcode;
}
public String getItemdescription() {
return Itemdescription;
}
public void setItemdescription(String itemdescription) {
Itemdescription = itemdescription;
}
public int getItemphoto() {
return Itemphoto;
}
public void setItemphoto(int itemphoto) {
Itemphoto = itemphoto;
}
public void setEnabled(boolean b) {
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
}
breakfast_service.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android">
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<listview> android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<Button
android:id="@+id/button12"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="@string/addtocart" />
list.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"> android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<linearlayout> android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="2dip"
android:removed="#336699" >
<ImageView
android:id="@+id/imageViewi"
android:layout_width="50dip"
android:layout_height="50dip"
android:src="@drawable/ic_launcher" />
<textview>
android:id="@+id/textViewi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:layout_marginTop="8dip"
android:text="Middle Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<checkbox>
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Checkbox" />