【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_;
}
}
続きを読む…»