码迷,mamicode.com
首页 > 其他好文 > 详细

查看拍卖物品

时间:2014-09-29 00:05:16      阅读:311      评论:0      收藏:0      [点我收藏+]

标签:des   android   style   blog   http   io   os   ar   java   

查看拍卖物品程序界面:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:gravity="center" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:orientation="horizontal" android:gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="@dimen/sub_title_margin"> <TextView android:id="@+id/view_titile" android:text="@string/view_succ" android:textSize="@dimen/label_font_size" 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 --> <ListView android:id="@+id/succList" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout>

  

查看流拍物品的Fragment代码:
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_titile); // 为返回按钮的单击事件绑定事件监听器 bnHome.setOnClickListener(new HomeListener(getActivity())); String action = getArguments().getString("action"); // 定义发送请求的URL String url = HttpUtil.BASE_URL + action; // 如果是查看流拍物品,修改标题 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); } }
上面代码通过HttpUtil.getRequest(url)与服务器进行交互,并将返回结果封装成JSONArray对象(本质是一个数组),方法有:
length();
optJSONArray();
optJSONObject();
optXXX();
本程序提供了一个JSONArrayAdapter类,本质是一个Adapter,用于将JSONArray数组进行包装,并作为ListView的Adapter。
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 (JSONException 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
		{
			// 获取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
		{
			return tv;
		}
	}
}

 效果图:bubuko.com,布布扣 


  

查看拍卖物品

标签:des   android   style   blog   http   io   os   ar   java   

原文地址:http://www.cnblogs.com/zhujiabin/p/3999109.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!