我想像一个应用程序创建保持applcation [英] i want to create keep applcation like one applicatiobn
本文介绍了我想像一个应用程序创建保持applcation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
package com.appsrox.smartpad.note;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
导入android.widget.Gallery;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import com.appsrox.smartpad.R;
import com.appsrox.smartpad.SmartPad;
import com.appsrox.smartpad.model.Category;
import com.appsrox.smartpad.model.Note;
/ * *
* @author appsrox.com
*
* /
abstract class BaseActivity扩展活动{
// private static final String TAG =BaseActivity;
protected ImageButton categoryBtn;
protected Spinner spinner;
protected EditText titleEdit;
protected EditText contentEdit;
protected ImageButton addItemBtn;
protected LinearLayout checklistLL;
受保护图库;
protected LayoutInflater inflater;
protected SQLiteDatabase db;
受保护注意事项;
protected 字体字体;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.note);
findViews();
inflater = getLayoutInflater();
db = SmartPad.db;
font = Typeface.createFromAsset(getAssets(), fonts / OpenSans-Semibold.ttf);
// 创建实例
long noteId = getIntent()。getLongExtra(Note.COL_ID, 0 );
if (noteId > 0 )
note = new 注意(noteId);
else
note = new 注意();
光标c = Category.list(db);
startManagingCursor(c);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
this ,
android。 R.layout.simple_spinner_item,
c,
new String [] {Category。 COL_NAME},
new int [] {android.R.id.text1});
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener( new AdapterView.OnItemSelectedListener(){
public void onItemSelected(AdapterView note.setCategoryId(id);
SmartPad.LASTSELECTED_CATEGORYID = id;
}
public void onNothingSelected(AdapterView}
});
}
@Override
protected void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
if (note.getId()> 0 )
outState.putLong(Note.COL_ID,note.getId());
}
@Override
受保护 void onResto reInstanceState(Bundle savedInstanceState){
super.onRestoreInstanceState(savedInstanceState);
if (savedInstanceState.containsKey(Note.COL_ID))
note.setId(savedInstanceState.getLong(Note.COL_ID)) ;
}
@Override
受保护 void onResume (){
super.onResume();
// 加载数据
if (note.getId()> 0 )
note.load(db);
// 初始化视图
reset();
}
受保护 void findViews(){
categoryBtn =(ImageButton)findViewById(R.id.category_btn);
spinner =(Spinner)findViewById(R.id.spinner);
titleEdit =(EditText)findViewById(R.id.title_et);
contentEdit =(EditText)findViewById(R.id.content_et);
addItemBtn =(ImageButton)findViewById(R.id.additem_btn);
checklistLL =(LinearLayout)findViewById(R.id.checklist_ll);
gallery =(图库)findViewById(R.id.gallery);
}
public void onClick(查看v){
switch (v.getId()){
case R.id.category_btn :
spinner.performClick();
break ;
case R.id.done_btn:
finish();
break ;
}
}
受保护 void reset( ){
SpinnerAdapter adapter = spinner.getAdapter();
int count = adapter.getCount();
long categoryId = note.getCategoryId()> 0? note.getCategoryId():getDefaultCategoryId();
for ( int i = 0 ; i< count; > if (adapter.getItemId(i)== categoryId){
spinner.setSelection(i);
break ;
}
}
titleEdit .setText(note.getTitle());
contentEdit.setText(note.getContent());
}
private long getDefaultCategoryId(){
SpinnerAdapter adapter = spinner.getAdapter();
int count = adapter.getCount();
switch (SmartPad.getDefaultCategoryOpt()){
case 1 :
for ( int i = 0 ; i < count; > if (adapter.getItemId(i)== SmartPad.LASTCREATED_CATEGORYID){
返回 SmartPad.LASTCREATED_CATEGORYID;
}
}
break ;
case 2 :
for ( int i = 0 ; i< count; > if (adapter.getItemId(i)== SmartPad.LASTSELECTED_CATEGORYID){
return SmartPad.LASTSELECTED_CATEGORYID;
}
}
break ;
}
返回 SmartPad.PUBLIC_CATEGORYID;
}
受保护 void persist(){
note.setTitle(titleEdit.getText()。toString());
note.setContent(contentEdit.getText( ).toString());
note.setId(note.persist(db));
}
@Override
protected void onPaus e(){
if (note.getId()> 0 || canSave())
persist();
super.onPause();
}
abstract boolean canSave();
}
解决方案
管理活动生命周期 [ ^ ]
package com.appsrox.smartpad.note;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.Gallery;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import com.appsrox.smartpad.R;
import com.appsrox.smartpad.SmartPad;
import com.appsrox.smartpad.model.Category;
import com.appsrox.smartpad.model.Note;
/**
* @author appsrox.com
*
*/
abstract class BaseActivity extends Activity {
//private static final String TAG = "BaseActivity";
protected ImageButton categoryBtn;
protected Spinner spinner;
protected EditText titleEdit;
protected EditText contentEdit;
protected ImageButton addItemBtn;
protected LinearLayout checklistLL;
protected Gallery gallery;
protected LayoutInflater inflater;
protected SQLiteDatabase db;
protected Note note;
protected Typeface font;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.note);
findViews();
inflater = getLayoutInflater();
db = SmartPad.db;
font = Typeface.createFromAsset(getAssets(), "fonts/OpenSans-Semibold.ttf");
// create instance
long noteId = getIntent().getLongExtra(Note.COL_ID, 0);
if (noteId > 0)
note = new Note(noteId);
else
note = new Note();
Cursor c = Category.list(db);
startManagingCursor(c);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
this,
android.R.layout.simple_spinner_item,
c,
new String[]{Category.COL_NAME},
new int[]{android.R.id.text1});
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView note.setCategoryId(id);
SmartPad.LASTSELECTED_CATEGORYID = id;
}
public void onNothingSelected(AdapterView }
});
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (note.getId() > 0)
outState.putLong(Note.COL_ID, note.getId());
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
if (savedInstanceState.containsKey(Note.COL_ID))
note.setId(savedInstanceState.getLong(Note.COL_ID));
}
@Override
protected void onResume() {
super.onResume();
// load data
if (note.getId() > 0)
note.load(db);
// initialize views
reset();
}
protected void findViews() {
categoryBtn = (ImageButton) findViewById(R.id.category_btn);
spinner = (Spinner) findViewById(R.id.spinner);
titleEdit = (EditText) findViewById(R.id.title_et);
contentEdit = (EditText) findViewById(R.id.content_et);
addItemBtn = (ImageButton) findViewById(R.id.additem_btn);
checklistLL = (LinearLayout) findViewById(R.id.checklist_ll);
gallery = (Gallery) findViewById(R.id.gallery);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.category_btn:
spinner.performClick();
break;
case R.id.done_btn:
finish();
break;
}
}
protected void reset() {
SpinnerAdapter adapter = spinner.getAdapter();
int count = adapter.getCount();
long categoryId = note.getCategoryId()>0 ? note.getCategoryId() : getDefaultCategoryId();
for (int i=0; i<count;> if (adapter.getItemId(i) == categoryId) {
spinner.setSelection(i);
break;
}
}
titleEdit.setText(note.getTitle());
contentEdit.setText(note.getContent());
}
private long getDefaultCategoryId() {
SpinnerAdapter adapter = spinner.getAdapter();
int count = adapter.getCount();
switch(SmartPad.getDefaultCategoryOpt()) {
case 1:
for (int i=0; i<count;> if (adapter.getItemId(i) == SmartPad.LASTCREATED_CATEGORYID) {
return SmartPad.LASTCREATED_CATEGORYID;
}
}
break;
case 2:
for (int i=0; i<count;> if (adapter.getItemId(i) == SmartPad.LASTSELECTED_CATEGORYID) {
return SmartPad.LASTSELECTED_CATEGORYID;
}
}
break;
}
return SmartPad.PUBLIC_CATEGORYID;
}
protected void persist() {
note.setTitle(titleEdit.getText().toString());
note.setContent(contentEdit.getText().toString());
note.setId(note.persist(db));
}
@Override
protected void onPause() {
if (note.getId() > 0 || canSave())
persist();
super.onPause();
}
abstract boolean canSave();
}
解决方案
Managing the Activity Lifecycle[^]
这篇关于我想像一个应用程序创建保持applcation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文