Activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Row.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="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/iv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="3" />
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3" />
</LinearLayout>
Myitem.java
package com.example.listviewex04;
public class Myitem {
private int icon;
private String name;
public Myitem(int icon, String name) {
super();
this.icon = icon;
this.name = name;
}
public int getIcon() {
return icon;
}
public void setIcon(int icon) {
this.icon = icon;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
MyListAdapter.java
package com.example.listviewex04;
import java.util.ArrayList;
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.TextView;
public class MyListAdapter extends BaseAdapter {
Context context;
LayoutInflater inflater;
int layout;
ArrayList<Myitem> src;
public MyListAdapter(Context context, int layout, ArrayList<Myitem> src){
this.context = context;
this.src = src;
this.layout = layout;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return this.src.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return this.src.get(position).getName();
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
// 한 행의 디자인과 데이터의 결합
System.out.println("getView() 호출 :" + position);
if(convertView == null){
convertView = inflater.inflate(layout, parent, false);
}
ImageView iv = (ImageView)convertView.findViewById(R.id.iv);
iv.setImageResource(src.get(position).getIcon());
TextView tv = (TextView)convertView.findViewById(R.id.tv);
tv.setText(src.get(position).getName());
Button btn = (Button)convertView.findViewById(R.id.btn);
btn.setText("선택");
return convertView;
}
}
package com.example.listviewex04;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ListView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<Myitem> list = new ArrayList<Myitem>();
Myitem myitem1 = new Myitem(R.drawable.ic_launcher, "홍길동");
Myitem myitem2 = new Myitem(R.drawable.ic_launcher, "박문수");
Myitem myitem3 = new Myitem(R.drawable.ic_launcher, "성춘향");
Myitem myitem4 = new Myitem(R.drawable.ic_launcher, "이몽룡");
Myitem myitem5 = new Myitem(R.drawable.ic_launcher, "임꺽정");
Myitem myitem6 = new Myitem(R.drawable.ic_launcher, "홍길동");
Myitem myitem7 = new Myitem(R.drawable.ic_launcher, "박문수");
Myitem myitem8 = new Myitem(R.drawable.ic_launcher, "성춘향");
Myitem myitem9 = new Myitem(R.drawable.ic_launcher, "이몽룡");
Myitem myitem10 = new Myitem(R.drawable.ic_launcher, "임꺽정");
Myitem myitem11 = new Myitem(R.drawable.ic_launcher, "홍길동");
Myitem myitem12 = new Myitem(R.drawable.ic_launcher, "박문수");
Myitem myitem13 = new Myitem(R.drawable.ic_launcher, "성춘향");
Myitem myitem14 = new Myitem(R.drawable.ic_launcher, "이몽룡");
Myitem myitem15 = new Myitem(R.drawable.ic_launcher, "임꺽정");
Myitem myitem16 = new Myitem(R.drawable.ic_launcher, "홍길동");
Myitem myitem17 = new Myitem(R.drawable.ic_launcher, "박문수");
Myitem myitem18 = new Myitem(R.drawable.ic_launcher, "성춘향");
Myitem myitem19 = new Myitem(R.drawable.ic_launcher, "이몽룡");
Myitem myitem20 = new Myitem(R.drawable.ic_launcher, "임꺽정");
list.add(myitem1);
list.add(myitem2);
list.add(myitem3);
list.add(myitem4);
list.add(myitem5);
list.add(myitem6);
list.add(myitem7);
list.add(myitem8);
list.add(myitem9);
list.add(myitem10);
list.add(myitem11);
list.add(myitem12);
list.add(myitem13);
list.add(myitem14);
list.add(myitem15);
list.add(myitem16);
list.add(myitem17);
list.add(myitem18);
list.add(myitem19);
list.add(myitem20);
MyListAdapter listAdapter = new MyListAdapter(this, R.layout.row, list);
ListView lv = (ListView)findViewById(R.id.lv);
lv.setAdapter(listAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
'Android' 카테고리의 다른 글
항목 뷰 - ListActivity (0) | 2013.04.01 |
---|---|
항목 뷰 - 확장리스트뷰 (0) | 2013.04.01 |
리스트뷰 - 항목 추가,삭제하기 (multiplechoice) (0) | 2013.04.01 |
리스트뷰 - 항목 추가, 삭제하기(singlechoice) (0) | 2013.04.01 |
리스트뷰 - choiceMode (0) | 2013.04.01 |