本来准备周末看NBA,可是这两天都没比赛,要到礼拜一,悲吹的上班族
最近来感觉进步缓慢,难道是天气越来越热的缘故,开玩笑了,今天冒雨来公司加班,接着完善这个的项目的其他的功能模块。
今天说的模块是:查看流拍的物品—(各位童鞋的可以参照李刚的书19章的项目,往大家拍砖)
它对应的Servlet是ViewFailServlet,返回的数据为:
JSON数据,类似如下:
[{"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"}]
1. 查看流拍的物品的程序界面:view_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"
>
<TextView
android:id="@+id/view_title"
android:text="@string/view_succ"
android:textSize="@dimen/label_font_size"
android:textColor="@color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<!-- 定义返回按钮 -->
<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
android:id="@+id/succList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
2.查看流拍物品的Activity:ViewItem.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.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class ViewItem extends Activity{
Button bnHome;
ListView succList;
TextView viewTitle;
private static final String TAG="ViewItem";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.view_item);
//获取界面上的组件
bnHome = (Button)findViewById(R.id.bn_home);
succList = (ListView)findViewById(R.id.succList);
viewTitle =(TextView) findViewById(R.id.view_title);
//为返回按钮绑定事件监听器
bnHome.setOnClickListener(new FinishListener(this));
String action = getIntent().getStringExtra("action");
//定义发送的URL
String url = HttpUtil.BASE_URL+action;
if(action.equals("ViewFailServlet")){
viewTitle.setText(R.string.view_fial);
}
try{
//向指定URL发送请求,并把服务器响应转化成JSONArry对象
JSONArray jsonArray = new JSONArray(HttpUtil.getRequest(url));
//将JSONArray包装成Adapter
JSONArrayAdapter jsonAdapter = new JSONArrayAdapter(ViewItem.this, jsonArray, "itemName", true);
succList.setAdapter(jsonAdapter);
}catch(Exception e){
DialogUtil.showDialog(this, "服务器响应异常,请稍后再试!",false);
e.printStackTrace();
}
succList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
//查看指定的物品
viewItemDetail(position);
}
});
}
//查看指定的物品
private void viewItemDetail(int postiton){
//加载detail.xml界面布局代表视图
View detailView = getLayoutInflater().inflate(R.layout.detail, null);
//获取detail.xml界面布局文件中的文本框
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 itemRemark =(EditText)detailView.findViewById(R.id.itemRemark);
//获取被单击的列表项
JSONObject jsonObject = (JSONObject)succList.getAdapter().getItem(postiton);
try{
//通过文本显示物品详情
Log.i(TAG, "itemKind--01>"+jsonObject.getString("itemName"));
Log.i(TAG, "itemKind--02>"+jsonObject.getDouble("maxPrice")+"");
Log.i(TAG, "itemKind--03>"+jsonObject.getString("itemDesc"));
itemName.setText(jsonObject.getString("itemName"));
itemKind.setText(jsonObject.getString("kind"));
// Log.i(TAG, "itemKind--01>"+jsonObject.getString("kind"));
maxPrice.setText(jsonObject.getDouble("maxPrice")+"");
itemRemark.setText(jsonObject.getString("itemDesc"));
}catch(JSONException e){
e.printStackTrace();
}
DialogUtil.showDialog(ViewItem.this, detailView);
}
}
3.这里上面用到了把一个JSON对象,转化成数组来处理,在对JSON数组进行包装,作为ListView的内容的Adapter的工具类:JSONArrayAdapter.java
package com.infy.auction.client.util;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.infy.auction.client.R;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class JSONArrayAdapter extends BaseAdapter{
private Context ctx;
//定义需要包装的JSONArray对象
private JSONArray jsonArray;
//定义列表显示JSONOBject对象的那个属性
private String property;
private boolean hasIcon;
public JSONArrayAdapter(Context ctx,JSONArray jsonArray,String property,boolean hasIcon) {
// TODO Auto-generated constructor stub
this.ctx = ctx;
this.jsonArray = jsonArray;
this.property = property;
this.hasIcon = hasIcon;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return jsonArray.length();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return jsonArray.optJSONObject(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
try{
return ((JSONObject)getItem(position)).getInt("id");
}catch(JSONException e){
e.printStackTrace();
}
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//定义一个线性布局管理器
LinearLayout linear = new LinearLayout(ctx);
//设置为水平的线性布局管理器
linear.setOrientation(0);
//创建一个ImageView
ImageView iv = new ImageView(ctx);
iv.setPadding(10, 0, 20, 0);
iv.setImageResource(R.drawable.item);
//将图片添加到LinearLayout中
linear.addView(iv);
//创建一个TextView
TextView tv = new TextView(ctx);
try{
//获取JSONArray数组元素的property属性
String itemName = ((JSONObject)getItem(position)).getString(property);
//设置显示的Textview所显示的内容
tv.setText(itemName);
}catch(JSONException e){
e.printStackTrace();
}
tv.setTextSize(20);
if(hasIcon){
//将TextView添加到LinearLayout
linear.addView(tv);
return linear;
}else{
tv.setTextColor(Color.BLACK);
return tv;
}
}
}
最后查看流拍物品的记录的效果图如下: