读书频道 > 网站 > 网页设计 > Android数据库应用编程——为企业开发数据驱动Android应用
1.4.5 PickFragment类
15-04-21    下载编辑
收藏    我要投稿   
学习为企业系统构建数据驱动的Android应用程序。Android设备已经超越消费应用范畴,进入企业领域。如果你准备着手构建与企业系统集成的数据驱动型Android应用程序,那么本书无疑是理想之选。 Android数据库应用立即去当当网订购
如果你不曾使用过Fragment,那么代码清单1-2中的代码看起来非常像一个Activity子类。但与活动的区别是,片段的生命周期与包含该片段的活动的生命周期相关联。在大屏幕布局中,这意味着当调用活动的生命周期方法时,该Activity中所有的Fragment对象都会调用自己相应的生命周期方法。

代码清单1-2:PickFragment.java
package com.enterpriseandroidbook.fragmentframework;
      
import android.app.Activity;
import android.app.Fragment;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class PickFragment extends Fragment implements OnItemClickListener {

// String for logging the class name
private final String CLASSNAME = getClass().getSimpleName();

// Turn logging on or off
private static final boolean L = true;

public void onAttach(Activity activity) {
super.onAttach(activity);

// Notification that the fragment is associated with an Activity
if (L)
Log.i(CLASSNAME, "onAttach " + activity.getClass().getSimpleName());
}

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Tell the system we have an options menu
this.setHasOptionsMenu(true);

if (null != savedInstanceState)
restoreState(savedInstanceState);

// Notification that
if (L)
Log.i(CLASSNAME, "onCreate");
}

// Factor this out of methods that get saved state
private void restoreState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
      
}
onCreateView方法调用attachAdapter和setOnItemClickListener,初始化该片段。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
      
final ListView list = (ListView) inflater.inflate(
R.layout.list_frag_list, container, false);
if (L)
Log.i(CLASSNAME, "onCreateView");
      
attachAdapter(list);
list.setOnItemClickListener(this);
      
return list;
}

public void onStart() {
super.onStart();
if (L)
Log.i(CLASSNAME, "onStart");
}

public void onresume() {
super.onResume();
if (L)
Log.i(CLASSNAME, "onResume");
}
      
public void onPause() {
super.onPause();
if (L)
Log.i(CLASSNAME, "onPause");
}

public void onStop() {
super.onStop();
if (L)
Log.i(CLASSNAME, "onStop");
}
      
public void onDestroyView() {
super.onDestroyView();
if (L)
Log.i(CLASSNAME, "onDestroyView");
}
      
public void onDestroy() {
super.onDestroy();
if (L)
Log.i(CLASSNAME, "onDestroy");
}
      
public void onDetach() {
super.onDetach();
if (L)
Log.i(CLASSNAME, "onDetach");
}
      
// /////////////////////////////////////////////////////////////////////
// Minor lifecycle methods
// /////////////////////////////////////////////////////////////////////
      
public void onActivityCreated() {
// Notification that the containing activiy and its View hierarchy exist
if (L)
Log.i(CLASSNAME, "onActivityCreated");
}
      
// /////////////////////////////////////////////////////////////////////
// Overrides of the implementations ComponentCallbacks methods in Fragment
// /////////////////////////////////////////////////////////////////////
      
@Override
public void onConfigurationChanged(Configuration newConfiguration) {
super.onConfigurationChanged(newConfiguration);
      
// This won't happen unless we declare changes we handle in the manifest
if (L)
Log.i(CLASSNAME, "onConfigurationChanged");
}
      
@Override
public void onLowMemory() {
// No guarantee this is called before or after other callbacks
if (L)
Log.i(CLASSNAME, "onLowMemory");
}
      
// /////////////////////////////////////////////////////////////////////
// Menu handling code
// /////////////////////////////////////////////////////////////////////
      
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.search_menu, menu);
}
      
// /////////////////////////////////////////////////////////////////////
// App-specific code
// /////////////////////////////////////////////////////////////////////
attachAdapter()方法用于追加一个ArrayAdapter到片段的ListView对象中。该ArrayAdapater包含此应用程序的测试值。
/**
* Attach an adapter that loads the data to the specified list
*
* @param list
*/
private void attachAdapter(final ListView list) {
      
// Make a trivial adapter that loads an array of strings
ArrayAdapter<String> numbers = new ArrayAdapter<String>(list
.getContext().getApplicationContext(),
android.R.layout.simple_list_item_1, new String[] { "one",
"two", "three", "four", "five", "six" });
      
// tell the list to use it
list.setAdapter(numbers);
// l.setOnItemClickListener(this);
}
      
// /////////////////////////////////////////////////////////////////////
// Implementation of the OnItemClickListener interface
// /////////////////////////////////////////////////////////////////////

onItemClick()方法实现onItemClickListener接口。这意味着每当单击Android项目时,就会调用onItemClick()方法。在这个示例中,整个片段就是ListView,单击列表中的项会导致一些数据被加载到屏幕右侧的片段中,或者,在小屏幕的情况下,则是一个单独的Activity。

 

@Override
public void onItemClick(AdapterView<?> arg0, View view, int position,
long id) {
// As an example of sending data to our fragments, we will create a
// bundle
// with an int and a string, based on which view was clicked
Bundle data = new Bundle();
int ordinal = position + 1;
data.putInt("place", ordinal);
data.putString("placeName", Integer.toString(ordinal));
((TabbedActivity) getActivity()).loadTabFragments(data);
      
}
      
}

 

点击复制链接 与好友分享!回本站首页
分享到: 更多
您对本文章有什么意见或着疑问吗?请到论坛讨论您的关注和建议是我们前行的参考和动力  
上一篇:1.3 功能
下一篇:1.5 小结
相关文章
图文推荐
JavaScript网页动画设
1.9 响应式
1.8 登陆页式
1.7 主题式
排行
热门
文章
下载
读书

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训
版权所有: 红黑联盟--致力于做最好的IT技术学习网站