OnResumeQ

AAAftS 1 fflffiMffi ' ffiM startActivityOAM pickup MM ' M pickup ^ AMffilnAWAA SharedPreferences ^Affi ° ^M® ac01 MM0# > ASMAS AMftAAATO SharedPreferences ^AffiAWAftlt^ ° AfiTOAAm '

- AAAftS 2~5 fflffiMffi ' M startActivityForResultOATO

pickup MM ' - MAMM startActivity()^MMAMftK

® ' AMMA^ ? AAA^A : #M onResume()^ onStartOAM ° AffiMAA IHAfiftSA °

MA-MMffiH°

MA-fflffiMffiH °

---- §AA 2 : pressPickup()^A^M#^^M±m Menu ^M<Pick Up An Item>Ml - faM ' ac01 tfrA^PE^f^ Android g^gm startActivity()® M!!A(WM)pickup Activity mAmftmW* '

---- §AA 3 : pickUpAnItem()^ATO#i£ pu_layout Menu ffllP! °

SharedPreferences fMfrffi® °

---- ffE 3.2 : pickup ^fr^g^m fimshOM ' MSfiffi^ ' ac01 °

---- ffE 3.2.1 : M® ac01 > g^MftW ac01 ^m onResume()®

m ' MWl^ SharedPreferences item ¿ft -

package com.misoo.ex01_06;

import android.app.Activity;

import android.content.Intent;

import android.content.SharedPreferences;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.TextView;

public class ac01 extends Activity implements OnClickListener { private boolean init_state = false; Button btn, btn2;

@Override public void onCreate(Bundle icicle) {

super.onCreate(icicle); init_state = true; setContentView(R.layout.acOi); btn = (Button)findViewById(R.id./>M_6/n);

btnsetBackgroundResource(R.drawable.x_/Mde); btn.setOnClickListener(this); btn2 = (Button)findViewById(R.id.exi/_6/n);

btn2.setBackgroundResource(R.drawable.x_/Mde); btn2.setOnClickListener(this); } @Override protected void onResume() { super.onResume();

if(init_state) { init_state = false; return; } SharedPreferences passwdfile = getSharedPreferences( "ITEM", 0); String im = passwdfile.getString("ITEM", null); TextView tv = (TextView)findViewById(R.id.tv); tv.setText( im ); } public void onClick(View v) {

if (v == btn){ Intent in = new Intent(ac01.this, pickup.class); start Activity (in); } if(v == btn2){ this.finish(); }

package com.misoo.ex01_06;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import android.app.ListActivity;

import android.content.SharedPreferences.Editor;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.widget.ListView;

import android.widget. SimpleAdapter;

public class pickup extends ListActivity {

static final int EXITID = Menu.F/^ST; private Map<String, Object> item; private List<Map<String, Object» coll; private int current = -1;

@Override public void onCreate(Bundle icicle) { super.onCreate(icicle);

coll = new ArrayList<Map<String, Object>>(); this.addData();

this.setListAdapter(new SimpleAdapter(this, coll, android.R.layout.simple_list_item_single_choice, new String[] { "title" }, new int[] {android.R.id.texti})); ListView lv = this.getListView(); lv.setItemsCanFocus(false);

lv.setBackgroundResource(R.drawable.gallery_photo_4); lv.setChoiceMode(ListView.CHOICEMODESINGLE); } @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); menu.add(0, EXITID, 2, "Exit"); MenuItem im = menu.findItem(EXIT_ID); im.setIcon(R.drawable. exit_em2); return true; }

@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case EXITID:

String str = coll.get(current).get("title").toString(); Editor passwdfile = getSharedPreferences("ITEM", 0).edit();

passwdfile .putString("ITEM", str); passwdfile .commit(); finish(); } break; }

return super.onOptionsItemSelected(item);

@Override protected void onListItemClick(ListView l, View v, int position, long id) {

current = position; } protected void addData() {

item.put("title", "Item-1"); item.put("price", "US$50"); coll.add(item); item = new HashMap<String, Object>();

item.put("title", "Item-2"); item.put("price", "US$800"); coll.add(item); item = new HashMap<String, Object>();

item.put("title", ' 'Item-3"); item.put("price", ' 'US$777"); coll.add(item);

Affl ac01 .xml ft^AMA-fflffiM^ 6.5.5 ®ft ac01.xml ' S

0 0

Post a comment