note

ListIcon (ListView에 아이콘,버튼) 본문

Android/기본

ListIcon (ListView에 아이콘,버튼)

투한 2012. 1. 18. 16:47






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

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>


widget_icontext.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent" 
	android:layout_height="60px"
	android:padding="5px">
	<ImageView 
	    android:layout_width="wrap_content"
		android:layout_height="wrap_content" 
		android:id="@+id/img"
		android:layout_alignParentLeft="true" />
	<TextView 
	    android:layout_width="wrap_content"
		android:layout_height="wrap_content" 
		android:id="@+id/text"
		android:textColor="#00ff00" 
		android:textSize="30sp"
		android:layout_toRightOf="@id/img" />
	<Button android:layout_width="wrap_content"
		android:layout_height="wrap_content" 
		android:id="@+id/btn"
		android:layout_alignParentRight="true" 
		android:text="주문" />
</RelativeLayout>




package kr.android.list.icon;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.content.Context;
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 ListIconTest extends Activity {
	ArrayList arItem;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		arItem = new ArrayList();
		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, "대우 마티즈");
		arItem.add(mi);

		MyListAdapter myAdapter = new MyListAdapter(this,
				R.layout.widget_icontext, arItem);

		ListView myList;
		myList = (ListView) findViewById(R.id.list);
		myList.setAdapter(myAdapter);
	}
}

// 리스트 뷰에 출력할 항목
class MyItem {
	int icon;
	String name;

	MyItem(int icon, String name) {
		this.icon = icon;
		this.name = name;
	}
}

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

	public MyListAdapter(Context context, int alayout, ArrayList aarSrc) {
		maincon = context;
		arSrc = aarSrc;
		layout = alayout;
		// ListView에서 사용한 View를 정의한 xml 를 읽어오기 위해
		// LayoutInfalater 객체를 생성
		inflater = LayoutInflater.from(maincon);
	}

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

	public Object 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;
	}
}


'Android > 기본' 카테고리의 다른 글

SQLite 연동하기  (0) 2012.01.18
터치해서 화면 전환 Flipper Touch  (0) 2012.01.18
News Service 주기적으로 데이터 가지고 오기  (0) 2012.01.18
SMSReceiver (BroadcastReceiver사용)  (0) 2012.01.18
Notify 알림 메세지  (0) 2012.01.18