この記事は最終更新日から1年以上経過しています。
【Listactivity.java】
1 | public class ListActivity extends Activity { |
4 | public void onCreate(Bundle savedInstanceState) { |
5 | super .onCreate(savedInstanceState); |
6 | setContentView(R.layout.main); |
9 | List<CustomData> objects = new ArrayList<CustomData>(); |
12 | image = BitmapFactory.decodeFile(getResources(), R.drawable.ic_lancher); |
14 | CustomData item = new CustomData(); |
18 | for ( int i = 1 ; i <= 3 ; i++ ) { |
20 | item.setNumber(String.valueOf(i)); |
24 | CustomAdapter customAdapter = new CustomAdapter( this , 0 , objects); |
25 | ListView listView = (ListView)findViewById(R.id.list); |
26 | listView.setAdapter(customAdapter); |
31 | class CustomAdapter extends ArrayAdapter<CustomData> { |
32 | private LayoutInflater layoutInflater_; |
34 | public CustomAdapter(Context context, int textViewResourceId, List<CustomData> objects) { |
35 | super (context, textViewResourceId, objects); |
36 | layoutInflater_ = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
40 | public View getView( int position, View convertView, ViewGroup parent) { |
42 | CustomData item = (CustomData)getItem(position); |
44 | if ( null == convertView) { |
45 | convertView = layoutInflater_.inflate(R.layout.list_item, null ); |
50 | imageView = (ImageView)convertView.findViewById(R.id.imageView1); |
51 | imageView.setImageBitmap(item.getImage()); |
54 | textView = (TextView)convertView.findViewById(R.id._id); |
55 | textView.setText(String.valueOf(item.getId())); |
58 | textView2 = (TextView)convertView.findViewById(R.id.number); |
59 | textView2.setText(item.getNumber()); |
66 | private Bitmap image_; |
68 | private String number_; |
70 | public void setImage(Bitmap image) { |
73 | public Bitmap getImage() { |
77 | public void setId( int id) { |
84 | public void setNumber(String number) { |
87 | public String getNumber() { |
【main.xml】
1 | <? xml version = "1.0" encoding = "utf-8" ?> |
2 | < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" |
3 | android:layout_width = "fill_parent" |
4 | android:layout_height = "fill_parent" |
5 | android:orientation = "vertical" > |
9 | android:layout_width = "fill_parent" |
10 | android:layout_height = "fill_parent" /> |
【list_item.xml】
1 | <? xml version = "1.0" encoding = "utf-8" ?> |
2 | < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" |
3 | android:layout_width = "fill_parent" |
4 | android:layout_height = "fill_parent" |
5 | android:orientation = "horizontal" |
6 | android:padding = "8px" > |
9 | android:id = "@+id/imageView1" |
10 | android:layout_width = "80sp" |
11 | android:layout_height = "50sp" /> |
13 | < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" |
14 | android:layout_width = "fill_parent" |
15 | android:layout_height = "wrap_content" |
16 | android:orientation = "vertical" |
17 | android:layout_marginLeft = "6px" > |
21 | android:layout_width = "fill_parent" |
22 | android:layout_height = "wrap_content" |
23 | android:textSize = "15sp" /> |
26 | android:id = "@+id/number" |
27 | android:layout_width = "fill_parent" |
28 | android:layout_height = "wrap_content" |
29 | android:textSize = "15sp" /> |
ListView関連で参考にさせていただいたURL
http://techbooster.jpn.org/andriod/ui/1282/
http://lablog.lanche.jp/archives/220
http://slumbers99.blogspot.com/2011/08/android-listview.html
http://labs.techfirm.co.jp/android/cho/751
http://d.hatena.ne.jp/tomstay/20110421/1303391426
http://www.adakoda.com/android/000077.html