List MK

exD5

Item-1 Item-2 Item-3

2. MMAX<Item-3>IlgB# ' ĮffS(Tltle)MHAAAA : "choice: i00.25" =

choice: 305.5

Prod-x

Prod-y

Prod-z

4. MM<Prod-x>Ilg0# ' <®ffS(Title)MHXXW : "choice: 305.5" =

package com.misoo.ex05;

import android.app.Activity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.widget.ArrayAdapter;

import android.widget.ListView;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemClickListener;

public class ex05 extends Activity implements OnItemClickListener { public static final int RELOADID = Menu.FIRST; public static final int EXITID = Menu.FIRST + 1; List View lv; Array Adapter<String> adapter; DataModel dm; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); dm = new DataModel(); lv = new ListView(this);

adapter = new ArrayAdapter<String>

(this, android.R.layoutsimple_list_item_1, dm.loadData()); lv. setAdapter(adapter); lv.setOnltemClickListener(this); setContentView(lv);

public void onItemClick(AdapterView<?> arg0, View argl, int arg2, long arg3) {

setTitle("choice: " + String.valueO/(dm.getPrice(arg2))); } @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); menu.add(0, RELOADID, 0, "Reload"); Menultem im = menu.findItem(RELOAD_ID); im.setIcon(R.drawable. reloadim); menu.add(0, EXITID, l, "Exit"); im = menu.findItem(EXIT_ID); im. setIcon(R. drawable. exitim); return true; }

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

adapter = new ArrayAdapter<String>

(this, android.R.layout.simple_list_item_1, dm.loadData()); lv. setAdapter(adapter); break; case EXITID: finish(); break; } return super.onOptionsItemSelected(item);

package com.misoo.ex05; public class DataModel {

private String[] data, xdata; private double[] price; private boolean k = true; public DataModel() {

data[0] = "Item-l"; data[l] = "Item-2"; data[2] = "Item-3";

xdata[0] = "Prod-x"; xdata[l] = "Prod-y"; xdata[2] = "Prod-z"; price = new double[3];

price [0] = 305.5; price[1] = 56.75; price[2] = 100.25;

if(k) return xdata; else return data; } public double getPrice(int i) { return price[i]; }

1. MVC(model-view-controller)ftX^S >

5. Mg ' MM^ffl DataModel ^^ ' Mffifflfg^ : dm = new DataModel(); AHA—® DataModel ft^fr =

6. ' ffA : adapter = new ArrayAdapter<String>

(this, android.R.layout.simple_list_item_1, dm.loadData());

MA—® ArrayAdapter fti^fr > MW dm.loadDataOSmTOOT^M ft^W ' #Aadapter)® -

7. ffA : lv.setAdapter(adapter);

% lv WfrMlfa adapter ^frTOMMW °

8. ffA : lv.setOnltemClickListener(this);

rn^nrn ' ' mmmm^ftrn m ° n^ft^m ■ this mmmmwrnfrmnmrnm ex05 mmftmfr •

ISfftftfr'MMi ex05

OnltemClickListener AffiP^SAft onltemClick ()®mA,8SA °

9. ffA : setContentView(lv);

0 0

Post a comment