标签:android blog http io 使用 ar java sp div
android查看物品的种类,首先通过HttpUtil向服务器端发送请求,并把服务器端响应的字符串转化成JSONArray对象,再使用Adapter包装JSONArray对象,并使用ListView显示。
查看物品的种类的布局界面:
<?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"> <LinearLayout android:orientation="vertical" android:gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:text="@string/manage_kind" android:textSize="@dimen/label_font_size" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <!-- 添加种类的按钮 --> <Button android:id="@+id/bnAdd" android:layout_width="85dp" android:layout_height="30dp" android:background="@drawable/add_kind"/> </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/kindList" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout>
显示物品种类的Fragment代码:
public class ManageKindFragment extends Fragment
{ public static final int ADD_KIND = 0x1007; Button bnHome , bnAdd; ListView kindList; Callbacks mCallbacks; @Override public View onCreateView(LayoutInflater inflater , ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.manage_kind , container , false); // 获取界面布局上的两个按钮 bnHome = (Button) rootView.findViewById(R.id.bn_home); bnAdd = (Button) rootView.findViewById(R.id.bnAdd); kindList = (ListView) rootView.findViewById(R.id.kindList); // 为返回按钮的单击事件绑定事件监听器 bnHome.setOnClickListener(new HomeListener(getActivity())); // 为添加按钮的单击事件绑定事件监听器 bnAdd.setOnClickListener(new OnClickListener() { @Override public void onClick(View source) { // 当添加按钮被单击时,调用该Fragment所在Activity的onItemSelected方法 mCallbacks.onItemSelected(ADD_KIND , null); } }); // 定义发送请求的URL String url = HttpUtil.BASE_URL + "viewKind.jsp"; try { // 向指定URL发送请求,并把响应包装成JSONArray对象 final JSONArray jsonArray = new JSONArray( HttpUtil.getRequest(url)); // 把JSONArray对象包装成Adapter kindList.setAdapter(new KindArrayAdapter(jsonArray , getActivity())); } catch (Exception e) { DialogUtil.showDialog(getActivity() , "服务器响应异常,请稍后再试!" ,false); e.printStackTrace(); } return rootView; } // 当该Fragment被添加、显示到Activity时,回调该方法 @Override public void onAttach(Activity activity) { super.onAttach(activity); // 如果Activity没有实现Callbacks接口,抛出异常 if (!(activity instanceof Callbacks)) { throw new IllegalStateException( "ManageKindFragment所在的Activity必须实现Callbacks接口!"); } // 把该Activity当成Callbacks对象 mCallbacks = (Callbacks) activity; } // 当该Fragment从它所属的Activity中被删除时回调该方法 @Override public void onDetach() { super.onDetach(); // 将mCallbacks赋为null。 mCallbacks = null; } }
标签:android blog http io 使用 ar java sp div
原文地址:http://www.cnblogs.com/zhujiabin/p/4001281.html