결과화면

//ListViewDemo5.java

 

package com.example.containerdemo;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ListViewDemo5 extends Activity {
 ArrayList<MyItem> arItem;

 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.list5);

  arItem = new ArrayList<MyItem>();
  MyItem mi;
  mi = new MyItem(R.drawable.ic_launcher, "삼성 노트북");
  arItem.add(mi);
  mi = new MyItem(R.drawable.ic_launcher, "LG 노트북");
  arItem.add(mi);
  mi = new MyItem(R.drawable.ic_launcher, "HP 노트북");
  arItem.add(mi);

  MyListAdapter MyAdapter = new MyListAdapter(this, R.layout.row2, arItem);

  ListView MyList;
  MyList = (ListView) findViewById(R.id.list);
  MyList.setAdapter(MyAdapter);
 }
}

// 리스트 뷰에 출력할 항목
class MyItem {
 MyItem(int aIcon, String aName) {
  Icon = aIcon;
  Name = aName;
 }

 int Icon;
 String Name;
}

// 어댑터 클래스
class MyListAdapter extends BaseAdapter {
 Context maincon;
 LayoutInflater Inflater;
 ArrayList<MyItem> arSrc;
 int layout;

 public MyListAdapter(Context context, int alayout, ArrayList<MyItem> aarSrc) {
  maincon = context;
  Inflater = (LayoutInflater) context
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  arSrc = aarSrc;
  layout = alayout;
 }

 public int getCount() {
  return arSrc.size();
 }

 public String getItem(int position) {
  return arSrc.get(position).Name;
 }

 public long getItemId(int position) {
  return position;
 }

 // 각 항목의 뷰 생성
 public View getView(int position, View convertView, ViewGroup parent) {
  final int pos = position;
  if (convertView == null) {
   convertView = Inflater.inflate(layout, parent, false);
  }
  ImageView img = (ImageView) convertView.findViewById(R.id.img);
  img.setImageResource(arSrc.get(position).Icon);

  TextView txt = (TextView) convertView.findViewById(R.id.text);
  txt.setText(arSrc.get(position).Name);

  Button btn = (Button) convertView.findViewById(R.id.btn);
  btn.setOnClickListener(new Button.OnClickListener() {
   public void onClick(View v) {
    String str = arSrc.get(pos).Name + "를 주문합니다.";
    Toast.makeText(maincon, str, Toast.LENGTH_SHORT).show();
   }
  });

  return convertView;
 }
}

 

 

 

// row.xml

 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="60dip"
 android:padding="5dip">
 <ImageView
  android:id="@+id/img"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerVertical="true"
  android:layout_alignParentLeft="true" />
 <TextView
  android:id="@+id/text"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:textColor="#00ff00"
  android:textSize="26sp"
  android:layout_centerVertical="true"
  android:layout_toRightOf="@id/img" />
 <Button
  android:id="@+id/btn"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerVertical="true"
  android:layout_alignParentRight="true"
  android:text="주문" />
</RelativeLayout>

 

 

 

 

//list5.xml

 

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
   
<ListView
 android:id="@+id/list" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
 />
</LinearLayout>

 

 

'2020년도 이전 > [WebSig] Android' 카테고리의 다른 글

GridView Demo  (0) 2013.08.14
ExpandableList ViewDemo  (0) 2013.08.14
ListView Demo3 - 리스트 추가, 삭제  (0) 2013.08.14
ListView Demo2 - extends ListActivity 이용...  (0) 2013.08.14
ListView Demo1  (0) 2013.08.14

+ Recent posts