application全局变量
16lz
2022-06-18
注意添加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()); }}
更多相关文章
- Android(安卓)Jetpack Compose 之 Text
- Android(安卓)TextView 给文本中指定片段添加自定义点击事件
- Android(安卓)M 去除抽屉模式
- Android在程序中动态生成控件,动态布局
- [Android(安卓)Pro] Notification的使用
- android添加各种权限整理
- android ScrollView--Linearlayout可以上下拖动
- Android(安卓)Fragment页打开相册
- 启动 flutter项目时报Could not find com.android.tools.build:g