package
com.amaker.wlo;
import
java.io.InputStream; import
java.net.URL; import
java.net.URLConnection; import
java.util.ArrayList; import
java.util.HashMap; import
java.util.List;
import
javax.xml.parsers.DocumentBuilder; import
javax.xml.parsers.DocumentBuilderFactory;
import
org.w3c.dom.Document; import
org.w3c.dom.NodeList;
import
android.app.Activity; import
android.content.Context; import
android.content.Intent; import
android.content.SharedPreferences; import
android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import
com.amaker.util.HttpUtil;
public
class MenuActivity
extends Activity {
private
ListView listview;
private
TextView totalPrice;
private
TextView totalFood;
private
Button callBtn;
private
Button cancleBtn;
@Override
protected
void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
listview=(ListView)findViewById(android.R.id.list);
totalPrice=(TextView)findViewById(R.id.price);
totalFood=(TextView)findViewById(R.id.foodname);
callBtn=(Button)findViewById(R.id.call);
callBtn.setOnClickListener(callListener);
cancleBtn=(Button)findViewById(R.id.cancelButton);
cancleBtn.setOnClickListener(canclelistener);
Context context= null ;
try {
context= createPackageContext( "com.amaker.wlo" , Context.CONTEXT_IGNORE_SECURITY);
} catch
(NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SharedPreferences preferences = context.getSharedPreferences( "shop_id" , MODE_WORLD_WRITEABLE);
int shopid=preferences.getInt( "shopid" ,
0 );
String urlStr = HttpUtil.BASE_URL + "servlet/FoodServlet?shopId=" +shopid;
try {
// 实例化URL
URL url =
new URL(urlStr);
// URLConnection 实例
URLConnection conn = url.openConnection();
// 获得输入流
InputStream in = conn.getInputStream();
// 获得DocumentBuilderFactory对象
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
// 获得DocumentBuilder对象
DocumentBuilder builder = factory.newDocumentBuilder();
// 获得Document对象
Document doc = builder.parse(in);
// 获得节点列表
NodeList nl = doc.getElementsByTagName(
"food" );
// Spinner数据
ArrayList<HashMap<String, Object>> listitem =
new
ArrayList<HashMap<String, Object>>();
// 获得XML数据
for ( int
i = 0 ; i < nl.getLength(); i++) {
// 商家編號
// 商家名字foodPrice String foodName = doc.getElementsByTagName( "foodName" )
.item(i).getFirstChild().getNodeValue();
String foodPrice = doc.getElementsByTagName( "foodPrice" )
.item(i).getFirstChild().getNodeValue();
HashMap<String, Object> map = new
HashMap<String, Object>();
map.put( "foodName" , foodName);
map.put( "foodPrice" , foodPrice+ "元" );
listitem.add(map);
}
MenuListAdapter listAdapter = new
MenuListAdapter( this ,listitem, R.layout.listview_style,
new String[] {
"foodName" , "foodPrice"
},
new int [] { R.id.topTextView, R.id.bottomTextView }
);
listview.setAdapter(listAdapter);
listview.setOnItemClickListener(getMenu);
} catch
(Exception e) {
e.printStackTrace();
}
}
OnItemClickListener getMenu= new
OnItemClickListener() {
int sumprice= 0 ;
//定义一个存储所有价格的变量
String sumfood=
"" ;
//定义一个存储所有食物的变量
@Override
public void
onItemClick(AdapterView<?> parent, View view,
int
position,
long id) {
RelativeLayout lr = (RelativeLayout) view;
TextView tvtop = (TextView) lr.getChildAt( 1 );
String foodName=tvtop.getText().toString()+ " " ;
TextView tv = (TextView) lr.getChildAt( 2 );
String s = tv.getText().toString();
int foodprice=Integer.parseInt(s.substring( 0 , s.indexOf( "元" )));
// 取得ViewHolder对象,这样就省去了通过层层的findViewById去实例化我们需要的cb实例的步骤
ViewHolder holder = (ViewHolder) view.getTag();
// 改变CheckBox的状态
holder.cb.toggle();
// 将CheckBox的选中状况记录下来
MenuListAdapter.getIsSelected().put(position, holder.cb.isChecked());
// 调整选定条目
if (holder.cb.isChecked() == true ) {
sumprice=sumprice+foodprice;
sumfood=sumfood+foodName;
} else
{
sumprice=sumprice-foodprice;
sumfood=sumfood.replace(foodName, "" );
}
// 用TextView显示
totalPrice.setText(
"你預計消費: " +sumprice);
totalFood.setText( "你已經點了: " +sumfood);
}
};
OnClickListener callListener= new
OnClickListener() {
@Override
public void
onClick(View v) {
Context context= null ;
try {
context= createPackageContext( "com.amaker.wlo" , Context.CONTEXT_IGNORE_SECURITY);
} catch
(NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SharedPreferences pref = context.getSharedPreferences( "phoneinfo" , MODE_WORLD_WRITEABLE);
String PhoneNum=pref.getString( "PhoneNum" ,
"" );
Intent intent = new
Intent(Intent.ACTION_CALL,Uri.parse( "tel:" +PhoneNum));
startActivity(intent);
}
};
OnClickListener canclelistener= new
OnClickListener() {
@Override
public void
onClick(View v) {
Intent intent= new
Intent(MenuActivity. this ,MainMenuActivity. class );
startActivity(intent);
}
};
}
|