package org.crazyit.auction.client;
import java.util.HashMap;
import java.util.Map;
import org.crazyit.auction.client.util.DialogUtil;
import org.crazyit.auction.client.util.HttpUtil;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
/**
* Description:
*
site:
crazyit.org
*
Copyright (C), 2001-2014, Yeeku.H.Lee
*
This program is protected by copyright laws.
*
Program Name:
*
Date:
* @author Yeeku.H.Lee
[email protected]
* @version 1.0
*/
public class AddItemFragment extends Fragment
{
// 定义界面中文本框
EditText itemName, itemDesc,itemRemark,initPrice;
Spinner itemKind , availTime;
// 定义界面中两个按钮
Button bnAdd, bnCancel;
public static final int RESULT = 0x100a;
Callbacks mCallbacks;
@Override
public View onCreateView(LayoutInflater inflater
, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.add_item
, container , false);
// 获取界面中的文本框
itemName = (EditText) rootView.findViewById(R.id.itemName);
itemDesc = (EditText) rootView.findViewById(R.id.itemDesc);
//itemRemark = (EditText) rootView.findViewById(R.id.itemRemark);
initPrice = (EditText) rootView.findViewById(R.id.initPrice);
itemKind = (Spinner) rootView.findViewById(R.id.itemKind);
//availTime = (Spinner) rootView.findViewById(R.id.availTime);
// 定义发送请求的地址
//String url = HttpUtil.BASE_URL + "viewKind.jsp";
bnAdd = (Button) rootView.findViewById(R.id.bnAdd);
bnCancel = (Button) rootView.findViewById(R.id.bnCancel);
// 为取消按钮的单击事件绑定事件监听器
bnCancel.setOnClickListener(new HomeListener(getActivity()));
bnAdd.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
mCallbacks.onItemSelected(RESULT , null);
/*// 执行输入校验
if (validate())
{
// 获取用户输入的物品名、物品描述等信息
//String name = itemName.getText().toString();
//String desc = itemDesc.getText().toString();
//String remark = itemRemark.getText().toString();
//String price = initPrice.getText().toString();
//JSONObject kind = (JSONObject) itemKind.getSelectedItem();
int avail = availTime.getSelectedItemPosition();
//根据用户选择有效时间选项,指定实际的有效时间
switch(avail)
{
case 5 :
avail = 7;
break;
case 6 :
avail = 30;
break;
default :
avail += 1;
break;
}
try
{
// 添加物品
String result = null;
// = addItem(name, desc
// , remark , price , kind.getInt("id"));
// 显示对话框
DialogUtil.showDialog(getActivity()
, result , true);
}
catch (Exception e)
{
DialogUtil.showDialog(getActivity()
, "服务器响应异常,请稍后再试!" , false);
e.printStackTrace();
}
}*/
}
});
JSONArray jsonArray = null;String url = HttpUtil.BASE_URL + "getItem.jsp?itemId="
+ getArguments().getInt("itemId");
try
{
JSONObject jsonObj =createJSONObject();
JSONObject jsonObject1 =createJSONObject();
// 向指定URL发送请求,并将服务器响应包装成JSONArray对象。
jsonArray = new JSONArray();
jsonArray.put(jsonObj);
jsonArray.put(jsonObject1);
itemName.setText(jsonObj.getString("id"));
itemDesc.setText(jsonObj.getString("kindDesc"));
//itemRemark.setText(jsonObj.getString("remark"));
//itemKind.setText(jsonObj.getString("kind"));
initPrice.setText(jsonObj.getString("kindName"));
//maxPrice.setText(jsonObj.getString("maxPrice"));
//endTime.setText(jsonObj.getString("endTime"));
// 获取系统中所有的物品种类
// 向执行URL发送请求,并把服务器响应包装成JSONArray
//jsonArray = new JSONArray(HttpUtil.getRequest(url)); //①
}
catch (Exception e1)
{
e1.printStackTrace();
}
/* // 将JSONArray包装成Adapter
JSONArrayAdapter adapter = new JSONArrayAdapter(
getActivity() , jsonArray , "kindName" , false);
// 显示物品种类列表
itemKind.setAdapter(adapter);*/
// 获取界面中的两个按钮
return rootView;
}
// 对用户输入的物品名、起拍价格进行校验
/*private boolean validate()
{
String name = itemName.getText().toString().trim();
if (name.equals(""))
{
DialogUtil.showDialog(getActivity() , "物品名称是必填项!" , false);
return false;
}
String price = initPrice.getText().toString().trim();
if (price.equals(""))
{
DialogUtil.showDialog(getActivity() , "起拍价格是必填项!" , false);
return false;
}
try
{
// 尝试把起拍价格转换为浮点数
Double.parseDouble(price);
}
catch(NumberFormatException e)
{
DialogUtil.showDialog(getActivity() , "起拍价格必须是数值!" , false);
return false;
}
return true;
}*/
/*private String addItem(String name, String desc
, String remark , String initPrice , int kindId , int availTime)
throws Exception
{
// 使用Map封装请求参数
Map
map = new HashMap();
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 + "addItem.jsp";
// 发送请求
//return HttpUtil.postRequest(url , map);
return null;
}
*/
// 当该Fragment被添加、显示到Activity时,回调该方法
@Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
// 如果Activity没有实现Callbacks接口,抛出异常
if (!(activity instanceof Callbacks))
{
throw new IllegalStateException(
"ManagerItemFragment所在的Activity必须实现Callbacks接口!");
}
// 把该Activity当成Callbacks对象
mCallbacks = (Callbacks) activity;
}
// 当该Fragment从它所属的Activity中被删除时回调该方法
@Override
public void onDetach()
{
super.onDetach();
// 将mCallbacks赋为null。
mCallbacks = null;
}
// 当该Fragment被添加、显示到Activity时,回调该方法
/*@Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
// 如果Activity没有实现Callbacks接口,抛出异常
if (!(activity instanceof Callbacks))
{
throw new IllegalStateException(
"ManagerItemFragment所在的Activity必须实现Callbacks接口!");
}
// 把该Activity当成Callbacks对象
mCallbacks = (Callbacks) activity;
}
// 当该Fragment从它所属的Activity中被删除时回调该方法
@Override
public void onDetach()
{
super.onDetach();
// 将mCallbacks赋为null。
mCallbacks = null;
}
*/
// 创建JSONObject对象
private static JSONObject createJSONObject() {
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("id", "1");
jsonObject.put("kindDesc", "这里并不是很主流的产品");
jsonObject.put("kindName", "1111");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonObject;
}
}
要打开一个心得页面,但总是在throw new IllegalStateException("ManagerItemFragment所在的Activity必须实现Callbacks接口!");处报错