- Android
- 2011-11-29 - 更新:2011-12-09
この記事は最終更新日から1年以上経過しています。
【Listactivity.java】
public class ListActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // データの作成 List<CustomData> objects = new ArrayList<CustomData>(); Bitmap image; image = BitmapFactory.decodeFile(getResources(), R.drawable.ic_lancher); CustomData item = new CustomData(); item.setImage(image); for ( int i = 1; i <= 3; i++ ) { item.setId(i); item.setNumber(String.valueOf(i)); objects.add(item); } CustomAdapter customAdapter = new CustomAdapter(this, 0, objects); ListView listView = (ListView)findViewById(R.id.list); listView.setAdapter(customAdapter); } } class CustomAdapter extends ArrayAdapter<CustomData> { private LayoutInflater layoutInflater_; public CustomAdapter(Context context, int textViewResourceId, List<CustomData> objects) { super(context, textViewResourceId, objects); layoutInflater_ = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public View getView(int position, View convertView, ViewGroup parent) { // 特定の行(position)のデータを得る CustomData item = (CustomData)getItem(position); // convertViewは使い回しされている可能性があるのでnullの時だけ新しく作る if (null == convertView) { convertView = layoutInflater_.inflate(R.layout.list_item, null); } // CustomDataのデータをViewの各Widgetにセットする ImageView imageView; imageView = (ImageView)convertView.findViewById(R.id.imageView1); imageView.setImageBitmap(item.getImage()); TextView textView; textView = (TextView)convertView.findViewById(R.id._id); textView.setText(String.valueOf(item.getId())); TextView textView2; textView2 = (TextView)convertView.findViewById(R.id.number); textView2.setText(item.getNumber()); return convertView; } } class CustomData { private Bitmap image_; private int id_; private String number_; public void setImage(Bitmap image) { image_ = image; } public Bitmap getImage() { return image_; } public void setId(int id) { id_ = id; } public int getId() { return id_; } public void setNumber(String number) { number_ = number; } public String getNumber() { return number_; } }
【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>
【list_item.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="horizontal" android:padding="8px" > <ImageView android:id="@+id/imageView1" android:layout_width="80sp" android:layout_height="50sp" /> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_marginLeft="6px" > <TextView android:id="@+id/_id" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="15sp" /> <TextView android:id="@+id/number" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="15sp" /> </LinearLayout> </LinearLayout>
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
4,348 views