注意添加application android:name="YourAppName"
public class YourAppName extends Application { private List<Item> mItems; @Override public void onCreate() { super.onCreate(); mItems = getYourItemList(); } public List<Item> getItems() { return mItems; } }
public class DetailActivity extends Activity {
private YourAppName mApp;
private ImageButton mNextButton;
private ImageButton mBackButton;
private TextView mTitle;
private int mIndex;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
mApp = (YourAppName) getApplication();
/*This index should come from the intent */
mIndex = 2;
getWidgets();
populateWidgets();
}
private void getWidgets() {
mNextButton = (ImageButton)findViewById(R.id.next);
mTitle = (TextView)findViewById(R.id.title);
/* other findViewById */
mNextButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mIndex++; /*Notice that this will get a NPE.
You need to place a better logic here */
populateWidgets();
}
});
}
private void populateWidgets() {
Item item = mApp.getItemList().get(mItem);
mTitle.setText(item.getName());
}
}