Mm

Intent in = new Intent(ac01.this, pickup.class); startActivityForResult (in ,PICKUP_REQ UEST);

Activity : pickup ' AAMffiAM startActivityForResult ()giMB °

bundle.putString("DataKey", item.getTitle().toString()); Intent mIntent = new Intent(); mIntent.putExtras(bundle); setResult(RESULT_OK, mIntent);

AAWgMft setResult()^ item ft^ mIntent °

protected void onActivityResult(int requestCode, int resultCode,

String data, Bundle extras) {

data AAWWfflitft item ft7 °

6.3 #15: iM ListView Menu

MffiMMMffift pickup iffiffiMMM Menu M ° ffi«AB£M ListView ' MgAMA^ttftAS ' @

M ListView o

' ListView AMftMAft Menu I^MB °

' ListView AMftMAft Menu I

//------------------------------------------------------------------------

Step-3: MM Activity ftAS^ : pickup ' SMA®$PT : // — pickup.java MA® —

package com.misoo.ex01_03; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ListView;

import android.widget.SimpleAdapter;

import android.widget.AdapterView.OnltemClickListener;

public class pickup extends Activity { private ListView lv; private Map<String, Object> item; private List<Map<String, Object>> coll; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle);

coll = new ArrayList<Map<String, Object>>(); lv = new ListView(this); this.addData(); SimpleAdapter adapter = new SimpleAdapter(this, coll, android.R.layoutsimple_list_item_1, new String[] { "title" }, new int[] {android.R.id.text!}); lv. setAdapter(adapter); lv. setOnltemClickListener(listener); setContentView(lv); } OnItemClickListener listener = new OnItemClickListener() {

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { String str = coll.get(arg2).get("price").toString(); Bundle bundle = new Bundle(); bundle.putString("DataKey", str); Intent mIntent = new Intent(); mIntent.putExtras(bundle); setResult(RESULT_OK, mIntent);

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);

1. ^^ms 4 ' b«mmListView r ° im' ^m^w^^M^^ffis finish();

Use Case Affl -

' AA ac01 Button > M pickup List M

AMAft Menu MTOB

2. MAST<Pickup> ' AlAS :

3. MTOM-fflMIl ' MI AM Menu ®m<M> ' Mmm ac01 m*®

Step-1: MA Android ^^ : ex01_04 ° Step-2: mm Activity mAMW : ac01 >

package com.misoo.ex01_04x;

import android.app.Activity;

import android.content.Intent;

import android.graphics.Color;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.LinearLayout;

import android.widget.TextView;

public class ac01 extends Activity implements OnClickListener { static final int PICKUPREQUEST = 0; LinearLayout layout; Text View tv; Button btn, btn2; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); layout = new LinearLayout(this);

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(200, 45); layout. setOrientation(LinearLay out. VERTICAL); tv = new TextView(this); tv.setTextColor(Color. WHITE); layout. addView(tv, lp);

LinearLayoutLayoutParams lp2 = new LinearLayout.LayoutParams(80, 45); lp2.topMargin = 5; btn = new Button(this);

btn.setBackgroundResource(R.drawable.x_/Mde); btn.setText("Pickup"); btn.setTextColor(Color.RED); btn.setOnClickListener(this); layout.addView(btn, lp2); btn2 = new Button(this);

btn2.setBackgroundResource(R.drawable.x_/Mde); btn2.setText(" EXIT "); btn2.setOnClickListener(this); layout.addView(btn2, lp2); setContentView(layout); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == PICKUPREQUEST) { if (resultCode == RESULTCANCELED)

setTitle("Canceled..."); else if(resultCode == RESULT OK) {

String data_str = (String)data.getCharSequenceExtra("DataKey"); this.tv.setText(data_str); }

public void onClick(View v) {

if (v == btn){ Intent in = new Intent(ac01.this, pickup.class);

startActivityForResult(in,PICKUP_REQUEST); } if(v.equals(btn2)){ this.finish(); }

Step-3: MM Activity m^^ : pickup > fig^I^T : // — pickup.java g^I —

package com.misoo.ex01_04x; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.ListActivity; import android.content.Intent; 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.FIRST; 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.layoutsimple_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, EXIT ID, 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:

if(current > -1){ String str = coll.get(current).get("price").toString(); Bundle bundle = new Bundle(); bundle.putString("DataKey", "item#" +

String.valMeO/(current+1)+ ": " + str); Intent mIntent = new Intent(); mIntent.putExtras(bundle); setResult(RESULT_OK, mIntent); finish(); } break; }

return super.onOptionsItemSelected(item);

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

current = position; } protected void addData() { item = new HashMap<String, Object>();

item.put("title", "Item-1"); item.put("price", "US$50"); coll.add(item);

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);

0 0

Post a comment