当前位置: 代码迷 >> Android >> 指尖下的Android之实战篇(八)
  详细解决方案

指尖下的Android之实战篇(八)

热度:19   发布时间:2016-05-01 17:50:39.0
指尖上的Android之实战篇(八)

   正式写之前,先发点感慨,在这里mark下:毕业到现在正好一年整,来杭州正好一年,在公司自己学到了很多东西,虽然有时我们有点有组织,无纪律。但是每人都有颗上进的心,有收获,有伤感(不细说,你懂得)。谨以此篇作为在杭州写的最后一篇Blong,留作纪念,下个星期正式进驻魔都,等着我的会是什么那,祝福自己在魔都各方面都好的发展。加油。。。微笑

    这个小项目的就写到这里,就整体功能来说,还有一小部分,感兴趣的童鞋可以参照李刚老师的Android讲义将剩下的补充完整。因为这是个人的练手的项目,必然有很多不足,如果有什么问题,欢迎童鞋一起交流。

管理物品模块:

1. 管理拍卖物品:
其对应的Servlet: ViewOwnerItemServlet:
返回的JSON对象类似为:
[{"id":2,"endtime":"2012-04-02","initPrice":210,"addtime":"2012-03-21","itemName":"display card","bids":[],"maxPrice":210,"itemRemark":"Old video card","itemDesc":"Old video card, still can use","kind":"computer hardware"},{"id":3,"endtime":"2012-03-28","initPrice":21000,"addtime":"2012-03-21","itemName":"The old house","bids":[],"maxPrice":25000,"itemRemark":"Old house","itemDesc":"40 years old house","kind":"house property"}]

2.查看自己的拍卖物品对应的布局文件:manage_item.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout   xmlns:android="http://schemas.android.com/apk/res/android"   android:orientation="vertical"   android:gravity="center"   android:background="@drawable/bg"   android:layout_width="fill_parent"   android:layout_height="fill_parent">    <LinearLayout    android:orientation="horizontal"    android:gravity="center"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:layout_margin="@dimen/sub_title_margin"    >    <LinearLayout    android:orientation="vertical"    android:gravity="center"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:paddingLeft="12px"    >    <TextView    android:text="@string/manage_item"    android:textSize="@dimen/label_font_size"    android:textColor="@color/white"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    />    <Button    android:id="@+id/bnAdd"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:background="@drawable/add_item"    />    </LinearLayout>    <Button    android:id="@+id/bn_home"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_marginLeft="@dimen/label_font_size"    android:background="@drawable/home"    />    </LinearLayout> <!-- 显示物品列表的ListView --> <ListView android:id="@+id/itemList" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout> 


3.管理物品对应的Activity为:MangerItem.java

package com.infy.auction.client; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import com.infy.auction.client.util.DialogUtil; import com.infy.auction.client.util.HttpUtil; import com.infy.auction.client.util.JSONArrayAdapter; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.AdapterView.OnItemClickListener; public class ManageItem extends Activity{ Button bnHome; Button bnAdd; ListView itemList; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.manage_item); //获取界面上的组件 bnHome = (Button)findViewById(R.id.bn_home); bnAdd = (Button)findViewById(R.id.bnAdd); itemList = (ListView)findViewById(R.id.itemList); //添加按钮事件监听器 bnHome.setOnClickListener(new FinishListener(this)); bnAdd.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub //增加拍卖的物品 Intent intent = new Intent(ManageItem.this, AddItem.class); startActivity(intent); } }); //定义发送的URL String url = HttpUtil.BASE_URL +"ViewOwnerItemServlet"; try{ //向指定的URL发送请求 JSONArray jsonArray = new JSONArray(HttpUtil.getRequest(url)); //将服务器响应包装成Adapter JSONArrayAdapter adapter = new JSONArrayAdapter(ManageItem.this, jsonArray, "itemName", true); itemList.setAdapter(adapter); }catch(Exception e){ DialogUtil.showDialog(this, "服务器响应异常,请稍后再试!", false); e.printStackTrace(); } itemList.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int postion, long id) { // TODO Auto-generated method stub //加载详细的清单Item viewItemInBid(postion); } }); } private void viewItemInBid(int position){ //加载detail_in_bid.xml布局 View detailView = getLayoutInflater().inflate(R.layout.detail_in_bid, null); //获取detail_in_bid界面中的文本框 EditText itemName =(EditText)detailView.findViewById(R.id.itemName); EditText itemKind = (EditText)detailView.findViewById(R.id.itemKind); EditText maxPrice = (EditText)detailView.findViewById(R.id.maxPrice); EditText initPrice = (EditText)detailView.findViewById(R.id.initPrice); EditText endTime = (EditText)detailView.findViewById(R.id.endTime); EditText itemRemark = (EditText)detailView.findViewById(R.id.itemRemark); //获取被单击列表项的所包装的JSONObject JSONObject jsonObject = (JSONObject)itemList.getAdapter().getItem(position); try{ //通过文本框显示物品详情 itemName.setText(jsonObject.getString("itemName")); itemKind.setText(jsonObject.getString("kind")); maxPrice.setText(jsonObject.getString("maxPrice")); itemRemark.setText(jsonObject.getString("itemDesc")); initPrice.setText(jsonObject.getString("initPrice")); endTime.setText(jsonObject.getString("endtime")); }catch(JSONException e){ e.printStackTrace(); } DialogUtil.showDialog(ManageItem.this, detailView); } } 


4.添加拍卖物品的Servlet:对应的服务器端的Servlet;AddItemServlet.java

5.添加拍卖物品:add_item.xml

<?xml version="1.0" encoding="utf-8"?> <TableLayout   xmlns:android="http://schemas.android.com/apk/res/android"   android:orientation="vertical"   android:background="@drawable/bg"   android:layout_width="fill_parent"   android:layout_height="fill_parent"   android:stretchColumns="1">   <TextView   android:layout_width="fill_parent"   android:layout_height="wrap_content"   android:text="@string/add_item_title"   android:textSize="@dimen/label_font_size"   android:gravity="center"   android:padding="@dimen/title_padding"   android:textColor="@color/white"   />   <!-- 输入物品名称 -->   <TableRow>   <TextView   android:text="@string/item_name"   android:textSize="@dimen/label_font_size"     android:textColor="@color/white"   android:layout_width="wrap_content"   android:layout_height="wrap_content"   />   <EditText   android:id="@+id/itemName"   android:layout_width="fill_parent"   android:layout_height="wrap_content"   />   </TableRow>   <!-- 输入物品描述 -->   <TableRow>   <TextView   android:text="@string/item_desc"   android:textSize="@dimen/label_font_size"     android:textColor="@color/white"   android:layout_width="wrap_content"   android:layout_height="wrap_content"   />   <EditText   android:id="@+id/itemDesc"   android:layout_width="fill_parent"   android:layout_height="wrap_content"   />   </TableRow>     <!-- 输入物品的备注行 -->   <TableRow>   <TextView   android:text="@string/remark"   android:textSize="@dimen/label_font_size"     android:textColor="@color/white"   android:layout_width="wrap_content"   android:layout_height="wrap_content"   />   <EditText   android:id="@+id/itemRemark"   android:layout_width="fill_parent"   android:layout_height="wrap_content"   />   </TableRow>   <!-- 输入起拍的价格 -->   <TableRow>   <TextView   android:text="@string/init_price"   android:textSize="@dimen/label_font_size"     android:textColor="@color/white"   android:layout_width="wrap_content"   android:layout_height="wrap_content"   />   <EditText   android:id="@+id/initPrice"   android:layout_width="fill_parent"   android:layout_height="wrap_content"   />   </TableRow>   <!-- 选择有效地时间 -->   <TableRow>   <TextView   android:text="@string/avail_time"   android:textSize="@dimen/label_font_size"     android:textColor="@color/white"   android:layout_width="wrap_content"   android:layout_height="wrap_content"   />   <Spinner   android:id="@+id/availTime"   android:layout_width="fill_parent"   android:layout_height="wrap_content"   android:entries="@array/availTime"   />   </TableRow>   <!-- 选择物品种类 -->   <TableRow>   <TextView   android:text="@string/item_kind"   android:textSize="@dimen/label_font_size"     android:textColor="@color/white"   android:layout_width="wrap_content"   android:layout_height="wrap_content"   />   <Spinner   android:id="@+id/itemKind"   android:layout_width="fill_parent"   android:layout_height="wrap_content"   />   </TableRow>   <!-- 定义按钮 -->   <LinearLayout   android:layout_width="fill_parent"   android:layout_height="wrap_content"   android:gravity="center"   >   <Button   android:id="@+id/bnAdd"   android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:text="@string/add"   />   <Button   android:id="@+id/bnCancel"   android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:text="@string/cancel"   />   </LinearLayout>   </TableLayout> 


6.添加物品的Activity:AddItem.java

package com.infy.auction.client; import java.util.HashMap; import java.util.Map; import org.json.JSONArray; import org.json.JSONObject; import com.infy.auction.client.util.DialogUtil; import com.infy.auction.client.util.HttpUtil; import com.infy.auction.client.util.JSONArrayAdapter; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Spinner; public class AddItem extends Activity { private static final String TAG = "AddItem"; EditText itemNmae, itemDesc, itemRemark, initPrice; Spinner itemKind, avaiTime; Button bnAdd, bnCancel; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.add_item); // 获取界面的组件 itemNmae = (EditText) findViewById(R.id.itemName); itemDesc = (EditText) findViewById(R.id.itemDesc); itemRemark = (EditText) findViewById(R.id.itemRemark); initPrice = (EditText) findViewById(R.id.initPrice); itemKind = (Spinner) findViewById(R.id.itemKind); avaiTime = (Spinner) findViewById(R.id.availTime); // 定义发送请求的地址,获得物品种类的数量 String url = HttpUtil.BASE_URL + "ViewKindServlet"; JSONArray jsonArray = null; try { jsonArray = new JSONArray(HttpUtil.getRequest(url)); Log.i(TAG, "jsonArray-->" + jsonArray.toString()); } catch (Exception e1) { e1.printStackTrace(); } // 将JSonArray包装成Adapter JSONArrayAdapter adapter = new JSONArrayAdapter(AddItem.this, jsonArray, "kindName", false); // 显示品种物类的列表 itemKind.setAdapter(adapter); // 获得界面中的两个按钮 bnAdd = (Button) findViewById(R.id.bnAdd); bnCancel = (Button) findViewById(R.id.bnCancel); // 添加按钮的监听器事件 bnCancel.setOnClickListener(new FinishListener(this)); bnAdd.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub // 进行输入的校验 if (validate()) { // 获取用户输入的物品名和物品的描述信息等 String name = itemNmae.getText().toString().trim(); String desc = itemDesc.getText().toString().trim(); String remark = itemRemark.getText().toString().trim(); String price = initPrice.getText().toString().trim(); JSONObject kind = (JSONObject) itemKind.getSelectedItem(); int avail = avaiTime.getSelectedItemPosition(); // 根据用户选择有效时间选项,指定实际的 有效时间 switch (avail) { case 5: avail = 7; break; case 6: avail = 30; break; default: avail += 1; break; } try { // 添加物品 String result = addItem(name, desc, remark, price, kind .getInt("id"), avail); // 显示对话框 DialogUtil.showDialog(AddItem.this, result, true); } catch (Exception e) { DialogUtil.showDialog(AddItem.this, "服务器异常,请稍后再试!", false); e.printStackTrace(); } } } }); } // 对输入的物品名和起拍价格进行检验 private boolean validate() { String name = itemNmae.getText().toString().trim(); if (name.equals("")) { DialogUtil.showDialog(this, "物品名称是必须要填的!", false); return false; } String price = initPrice.getText().toString().trim(); if (price.equals("")) { DialogUtil.showDialog(this, "起拍价格是必须要填的!", false); return false; } try { // 尝试把起拍价格转化为浮点数 Double.parseDouble(price); } catch (NumberFormatException e) { DialogUtil.showDialog(this, "起拍价格必须是数值!", false); return false; } return true; } private String addItem(String name, String desc, String remark, String initPrice, int kindId, int availTime) throws Exception { // 使用Map封装请求参数 Map<String, String> map = new HashMap<String, String>(); map.put("itemName", name); map.put("itemDesc", desc); map.put("itemRemark", remark); map.put("initPrice", initPrice); map.put("kindId", kindId + ""); map.put("availTime", availTime + ""); // 发送请求的URL String url = HttpUtil.BASE_URL + "AddItemServlet"; return HttpUtil.postRequest(url, map); } } 


7.效果图:

  相关解决方案