二、浏览流拍物品
viewItem.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:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/sub_title_margin"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="@+id/view_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/view_succ"
android:textSize="@dimen/label_font_size" />
<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="match_parent"
android:layout_height="match_parent" >
ListView>
LinearLayout>
ViewItemFragment.java
package com.lq.auction;
public class ViewItemFragment extends Fragment {
Button bnHome;
ListView succList;
TextView viewTitle;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.view_item, container, false);
//获取界面上的“返回”按钮
bnHome = (Button) rootView.findViewById(R.id.bn_home);
succList=(ListView) rootView.findViewById(R.id.succList);
viewTitle = (TextView) rootView.findViewById(R.id.view_title);
//为“返回”按钮的单击事件绑定事件监听器
bnHome.setOnClickListener(new HomeListener(getActivity())); //getActivity()?
String action = getArguments().getString("action");
//定义发送请求的URL
String url = HttpUtil.BASE_URL + action; //为什么要这么做?getArguments()?
//如果是查看流拍物品,修改标题
if(action.equals("viewFail.jsp")){
viewTitle.setText(R.string.view_fail);
}
try {
//向指定的URL发送请求,并把服务器响应转换成JSONArray对象
JSONArray jsonArray = new JSONArray(HttpUtil.getRequest(url));
//将JSONArray包装成Adapter
JSONArrayAdapter adapter = new JSONArrayAdapter(getActivity()
, jsonArray, "name", true);
succList.setAdapter(adapter);
} catch (Exception e) {
DialogUtil.showDialog(getActivity(), "服务器异常,请稍后再试", false);
e.printStackTrace();
}
succList.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView> parent, View view, int position,
long id) {
//查看指定物品的详细情况
viewItemDetail(position);
}
});
return rootView;
}
private void viewItemDetail(int position) {
//加载detail.xml界面布局代表的视图
View detailView = getActivity().getLayoutInflater()
.inflate(R.layout.detail, null);
//获取detail.xml界面布局中的文本框
TextView itemName = (TextView) detailView.findViewById(R.id.itemName);
TextView itemKind = (TextView) detailView.findViewById(R.id.itemKind);
TextView maxPrice = (TextView) detailView.findViewById(R.id.maxPrice);
TextView itemRemark = (TextView) detailView.findViewById(R.id.itemRemark);
//获取被单击的列表项
JSONObject jsonObj = (JSONObject) succList.getAdapter().getItem(position);
try {
//通过文本框显示物品详情
itemName.setText(jsonObj.getString("name"));
itemKind.setText(jsonObj.getString("kind"));
maxPrice.setText(jsonObj.getString("maxPrice"));
itemRemark.setText(jsonObj.getString("desc"));
} catch (JSONException e) {
e.printStackTrace();
}
DialogUtil.showDialog(getActivity(), detailView);
}
}
HomeListener.java
package com.lq.auction;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
public class HomeListener implements OnClickListener {
private Activity activity;
public HomeListener(Activity activity)
{
this.activity = activity;
}
@Override
public void onClick(View source)
{
Intent i = new Intent(activity , AuctionClientActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
activity.startActivity(i);
}
}
detail.xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1">
<TableRow>
<TextView
android:text="@string/item_name"
android:textSize="@dimen/label_font_size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/itemName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/tv_show"/>
TableRow>
<TableRow>
<TextView
android:text="@string/item_kind"
android:textSize="@dimen/label_font_size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/itemKind"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/tv_show"/>
TableRow>
<TableRow>
<TextView
android:text="@string/win_price"
android:textSize="@dimen/label_font_size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/maxPrice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/tv_show"/>
TableRow>
<TableRow>
<TextView
android:text="@string/remark"
android:textSize="@dimen/label_font_size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/itemRemark"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/tv_show"/>
TableRow>
TableLayout>
JSONArrayAdapter.java
package com.lq.auction;
import org.json.JSONArray;
import org.json.JSONObject;
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) {
this.ctx = ctx;
this.jsonArray = jsonArray;
this.property = property;
this.hasIcon = hasIcon;
}
@Override
public int getCount() {
return jsonArray.length();
}
@Override
public Object getItem(int position) {
return jsonArray.optJSONObject(position);
}
@Override
public long getItemId(int position) {
try {
//返回物品的id
return((JSONObject) getItem(position)).getInt("id");
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//定义一个线性布局管理器
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 {
//获取JSONObject数组元素的property属性
String itemName = ((JSONObject)getItem(position)).getString(property);
//设置TextView所显示的内容
tv.setText(itemName);
} catch (Exception e) {
e.printStackTrace();
}
tv.setTextSize(20);
if(hasIcon)
{
//将TextView添加到LinearLayout中
linear.addView(tv);
return linear;
}
else
{
tv.setTextColor(Color.BLACK);
}
return tv;
}
}
ViewItem.java
package com.lq.auction;
import android.app.Fragment;
import android.os.Bundle;
public class ViewItem extends FragmentActivity{
//重写getFragment()方法,该Activity显示该方法返回的Fragment
@Override
public Fragment getFragment() {
ViewItemFragment fragment = new ViewItemFragment();
Bundle arguments = new Bundle();
arguments.putString("action", getIntent().getStringExtra("action"));
fragment.setArguments(arguments);
return fragment;
}
}