2010年1月29日金曜日

[Jython] MACでjythonをインストールしてみた。

この投稿へのリンク
MACでjythonをインストールしてみた。

jythonは「http://www.jython.org/」でダウンロードする。
今回ダウンロードしたファイルは「jython_installer-2.5.1.jar」

インストール開始。

$ java -jar jython_installer-2.5.1.jar


実行するとGUIインストール画面が出る。
ちなみにコンソールモードは

$ java -jar jython_installer-2.5.1.jar --console



インストールPATHを「/usr/local/jython2.5.1」にしたかったので
先に「/usr/local/jython2.5.1」を作って指定した。





これで完了。
軽くにテストしてみる。

[Android] Custom Component - DateTimePicker

この投稿へのリンク

DatePickerとTimePickerを利用したDateTimePickerというのを作ってみた。

作った理由は何故かDatePickerとTimePickerは存在するのに、
日付と時間を両方設定できるコンポーネントが見つからないからだ。実はあるのか?
別にCustomコンポーネントの作り方の勉強にもなると思ったので作ってみよう。

レイアウト [res/layout/datetime_picker.xml]

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical" android:gravity="center">
<DatePicker android:id="@+id/_babukuma_datetime_picker_date_picker"
android:layout_width="wrap_content" android:layout_height="wrap_content"></DatePicker>
<CheckBox android:id="@+id/_babukuma_datetime_picker_enable_time"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="enable Time"></CheckBox>
<TimePicker android:id="@+id/_babukuma_datetime_picker_time_picker"
android:layout_width="wrap_content" android:layout_height="wrap_content"></TimePicker>
</LinearLayout>


次は必要かと思われるアトリビュートを定義(とりあえずはこれぐらいかな) [res/values/attrs.xml]

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="DataTimePicker">
<attr name="enableTime" format="boolean" />
<attr name="year" format="integer" />
<attr name="month" format="integer" />
<attr name="day" format="integer" />
<attr name="hour" format="integer" />
<attr name="minute" format="integer" />
</declare-styleable>
</resources>


次はDateTimePickerのスース [DateTimePicker.java]
やってるのはほぼDatePickerとTimePickerの機能をラッピングするだけ。
それと下段のTimePickerをCheckBoxで表示/非表示させただけ。

/**
* http://babukuma.com
*/
package com.babukuma.android.view;

import java.util.Calendar;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.TimePicker;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TimePicker.OnTimeChangedListener;

import com.babukuma.android.test.R;

/**
* @author babukuma
*/
public class DateTimePicker extends LinearLayout {
/**
* @author babukuma
*/
public interface OnDateTimeChangedListener {
/**
* @param view
* The view associated with this listener.
* @param year
* The year that was set.
* @param monthOfYear
* The month that was set (0-11) for compatibility with
* {@link java.util.Calendar}.
* @param dayOfMonth
* The day of the month that was set.
* @param hourOfDay
* The current hour.
* @param minute
* The current minute.
*/
void onDateTimeChanged(DateTimePicker view, int year, int monthOfYear,
int dayOfMonth, int hourOfDay, int minute);
}

private OnDateTimeChangedListener onDateTimeChangedListener;

private final DatePicker datePicker;
private final CheckBox enableTimeCheckBox;
private final TimePicker timePicker;

/**
* @param context
*/
public DateTimePicker(final Context context) {
this(context, null);
}

/**
* @param context
* @param attrs
*/
public DateTimePicker(final Context context, final AttributeSet attrs) {
super(context, attrs);

// Layout
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.datetime_picker, this, true);

// Attribute
Calendar calendar = Calendar.getInstance();
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.DataTimePicker);
final int _currentYear = a.getInt(R.styleable.DataTimePicker_year,
calendar.get(Calendar.YEAR));
final int _currentMonth = a.getInt(R.styleable.DataTimePicker_month,
calendar.get(Calendar.MONTH));
final int _currentDay = a.getInt(R.styleable.DataTimePicker_day,
calendar.get(Calendar.DAY_OF_MONTH));
final int _currentHour = a.getInt(R.styleable.DataTimePicker_hour,
calendar.get(Calendar.HOUR_OF_DAY));
final int _currentMinute = a.getInt(R.styleable.DataTimePicker_minute,
calendar.get(Calendar.MINUTE));

// DatePicker
datePicker = (DatePicker) findViewById(R.id._babukuma_datetime_picker_date_picker);
datePicker.init(_currentYear, _currentMonth, _currentDay,
new OnDateChangedListener() {

@Override
public void onDateChanged(final DatePicker view,
final int year, final int monthOfYear,
final int dayOfMonth) {
if (onDateTimeChangedListener != null) {
onDateTimeChangedListener.onDateTimeChanged(
DateTimePicker.this, year, monthOfYear,
dayOfMonth, timePicker.getCurrentHour(),
timePicker.getCurrentMinute());
}
}
});

// Enable Time checkbox
enableTimeCheckBox = (CheckBox) findViewById(R.id._babukuma_datetime_picker_enable_time);
enableTimeCheckBox
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
timePicker.setEnabled(isChecked);
timePicker.setVisibility((enableTimeCheckBox
.isChecked() ? View.VISIBLE : View.INVISIBLE));
}
});

// TimePicker
timePicker = (TimePicker) findViewById(R.id._babukuma_datetime_picker_time_picker);
timePicker.setOnTimeChangedListener(new OnTimeChangedListener() {
@Override
public void onTimeChanged(final TimePicker view,
final int hourOfDay, final int minute) {
if (onDateTimeChangedListener != null) {
onDateTimeChangedListener.onDateTimeChanged(
DateTimePicker.this, datePicker.getYear(),
datePicker.getMonth(), datePicker.getDayOfMonth(),
hourOfDay, minute);
}
}
});
timePicker.setCurrentHour(_currentHour);
timePicker.setCurrentMinute(_currentMinute);
timePicker.setEnabled(enableTimeCheckBox.isChecked());
timePicker.setVisibility((enableTimeCheckBox.isChecked() ? View.VISIBLE
: View.INVISIBLE));
}

public void setOnDateTimeChangedListener(
OnDateTimeChangedListener onDateTimeChangedListener) {
this.onDateTimeChangedListener = onDateTimeChangedListener;
}

public void updateDateTime(int year, int monthOfYear, int dayOfMonth,
int currentHour, int currentMinute) {
datePicker.updateDate(year, monthOfYear, dayOfMonth);
timePicker.setCurrentHour(currentHour);
timePicker.setCurrentMinute(currentMinute);
}

public void updateDate(int year, int monthOfYear, int dayOfMonth) {
datePicker.updateDate(year, monthOfYear, dayOfMonth);
}

public void setIs24HourView(final boolean is24HourView) {
timePicker.setIs24HourView(is24HourView);
}

public int getYear() {
return datePicker.getYear();
}

public int getMonth() {
return datePicker.getMonth();
}

public int getDayOfMonth() {
return datePicker.getDayOfMonth();
}

public int getCurrentHour() {
return timePicker.getCurrentHour();
}

public int getCurrentMinute() {
return timePicker.getCurrentMinute();
}

public boolean enableTime() {
return enableTimeCheckBox.isChecked();
}
}


とりあえずはこれで必要な簡単な機能はできてると思う。
後でもっと完成度を上げよう。

下からは作ったDateTimePickerをテスト


テスト用レイアウト [res/layout/datetime_picker_test.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="wrap_content"
android:orientation="vertical">
<TextView android:id="@+id/datetime_picker_activity_text_view"
android:layout_width="fill_parent" android:layout_height="48dip"
android:textColor="#000000" android:background="#CCCCCC"
android:gravity="center" android:text="time"></TextView>
<com.babukuma.android.view.DateTimePicker
android:layout_width="wrap_content" android:layout_height="wrap_content"
year="2010" month="1" day="29" hour="14" minute="1" startYear="2000"
android:id="@+id/datetime_picker_activity_datetime_picker">
</com.babukuma.android.view.DateTimePicker>
</LinearLayout>


テスト用ソース [DateTimePickerActivity.java]

package com.babukuma.android.test;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

import com.babukuma.android.view.DateTimePicker;
import com.babukuma.android.view.DateTimePicker.OnDateTimeChangedListener;

public class DateTimePickerActivity extends Activity {
final DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.datetime_picker_test);

final TextView timeView = (TextView) findViewById(R.id.datetime_picker_activity_text_view);
final DateTimePicker dateTimePicker = (DateTimePicker) findViewById(R.id.datetime_picker_activity_datetime_picker);
dateTimePicker
.setOnDateTimeChangedListener(new OnDateTimeChangedListener() {
@Override
public void onDateTimeChanged(DateTimePicker view,
int year, int monthOfYear, int dayOfMonth,
int hourOfDay, int minute) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, monthOfYear, dayOfMonth, hourOfDay,
minute);
timeView.setText(dateFormat.format(calendar.getTime()));
}
});

Calendar calendar = Calendar.getInstance();
calendar.set(dateTimePicker.getYear(), dateTimePicker.getMonth(),
dateTimePicker.getDayOfMonth(),
dateTimePicker.getCurrentHour(), dateTimePicker
.getCurrentMinute());
timeView.setText(dateFormat.format(calendar.getTime()));
}
}


次回はDateTimePickerDialogを作ってみる。

[Android] Custom Component - DateTimePicker

この投稿へのリンク

DatePicker와 TimePicker를 이용하여 간단하게 만들어본 DateTimePicker

DatePicker와 TimePicker는 존재하지만, 왜인지 날짜와 시간을 한번에 설정할 수 있는 컨포넌트가 안보인다.
그래서 간단하게 만들어봤다.

우선 레이아웃 구성 [res/layout/datetime_picker.xml]

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical" android:gravity="center">
<DatePicker android:id="@+id/_babukuma_datetime_picker_date_picker"
android:layout_width="wrap_content" android:layout_height="wrap_content"></DatePicker>
<CheckBox android:id="@+id/_babukuma_datetime_picker_enable_time"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="enable Time"></CheckBox>
<TimePicker android:id="@+id/_babukuma_datetime_picker_time_picker"
android:layout_width="wrap_content" android:layout_height="wrap_content"></TimePicker>
</LinearLayout>


다음은 몇가지 필요할 것 같은 Attribute를 정의 [res/values/attrs.xml]

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="DataTimePicker">
<attr name="enableTime" format="boolean" />
<attr name="year" format="integer" />
<attr name="month" format="integer" />
<attr name="day" format="integer" />
<attr name="hour" format="integer" />
<attr name="minute" format="integer" />
</declare-styleable>
</resources>


다음으로는 DateTimePicker의 소스다. [DateTimePicker.java]
대부분이 기존의 DatePicker와 TimePicker가 가지고 있는 메소드를 그대로 사용하도록 했고,
단지 하단의 TimePicker를 CheckBox를 이용해서 활성화/비활성화 시킨 것 뿐이다.

/**
* http://babukuma.com
*/
package com.babukuma.android.view;

import java.util.Calendar;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.TimePicker;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TimePicker.OnTimeChangedListener;

import com.babukuma.android.test.R;

/**
* @author babukuma
*/
public class DateTimePicker extends LinearLayout {
/**
* @author babukuma
*/
public interface OnDateTimeChangedListener {
/**
* @param view
* The view associated with this listener.
* @param year
* The year that was set.
* @param monthOfYear
* The month that was set (0-11) for compatibility with
* {@link java.util.Calendar}.
* @param dayOfMonth
* The day of the month that was set.
* @param hourOfDay
* The current hour.
* @param minute
* The current minute.
*/
void onDateTimeChanged(DateTimePicker view, int year, int monthOfYear,
int dayOfMonth, int hourOfDay, int minute);
}

private OnDateTimeChangedListener onDateTimeChangedListener;

private final DatePicker datePicker;
private final CheckBox enableTimeCheckBox;
private final TimePicker timePicker;

/**
* @param context
*/
public DateTimePicker(final Context context) {
this(context, null);
}

/**
* @param context
* @param attrs
*/
public DateTimePicker(final Context context, final AttributeSet attrs) {
super(context, attrs);

// Layout
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.datetime_picker, this, true);

// Attribute
Calendar calendar = Calendar.getInstance();
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.DataTimePicker);
final int _currentYear = a.getInt(R.styleable.DataTimePicker_year,
calendar.get(Calendar.YEAR));
final int _currentMonth = a.getInt(R.styleable.DataTimePicker_month,
calendar.get(Calendar.MONTH));
final int _currentDay = a.getInt(R.styleable.DataTimePicker_day,
calendar.get(Calendar.DAY_OF_MONTH));
final int _currentHour = a.getInt(R.styleable.DataTimePicker_hour,
calendar.get(Calendar.HOUR_OF_DAY));
final int _currentMinute = a.getInt(R.styleable.DataTimePicker_minute,
calendar.get(Calendar.MINUTE));

// DatePicker
datePicker = (DatePicker) findViewById(R.id._babukuma_datetime_picker_date_picker);
datePicker.init(_currentYear, _currentMonth, _currentDay,
new OnDateChangedListener() {

@Override
public void onDateChanged(final DatePicker view,
final int year, final int monthOfYear,
final int dayOfMonth) {
if (onDateTimeChangedListener != null) {
onDateTimeChangedListener.onDateTimeChanged(
DateTimePicker.this, year, monthOfYear,
dayOfMonth, timePicker.getCurrentHour(),
timePicker.getCurrentMinute());
}
}
});

// Enable Time checkbox
enableTimeCheckBox = (CheckBox) findViewById(R.id._babukuma_datetime_picker_enable_time);
enableTimeCheckBox
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
timePicker.setEnabled(isChecked);
timePicker.setVisibility((enableTimeCheckBox
.isChecked() ? View.VISIBLE : View.INVISIBLE));
}
});

// TimePicker
timePicker = (TimePicker) findViewById(R.id._babukuma_datetime_picker_time_picker);
timePicker.setOnTimeChangedListener(new OnTimeChangedListener() {
@Override
public void onTimeChanged(final TimePicker view,
final int hourOfDay, final int minute) {
if (onDateTimeChangedListener != null) {
onDateTimeChangedListener.onDateTimeChanged(
DateTimePicker.this, datePicker.getYear(),
datePicker.getMonth(), datePicker.getDayOfMonth(),
hourOfDay, minute);
}
}
});
timePicker.setCurrentHour(_currentHour);
timePicker.setCurrentMinute(_currentMinute);
timePicker.setEnabled(enableTimeCheckBox.isChecked());
timePicker.setVisibility((enableTimeCheckBox.isChecked() ? View.VISIBLE
: View.INVISIBLE));
}

public void setOnDateTimeChangedListener(
OnDateTimeChangedListener onDateTimeChangedListener) {
this.onDateTimeChangedListener = onDateTimeChangedListener;
}

public void updateDateTime(int year, int monthOfYear, int dayOfMonth,
int currentHour, int currentMinute) {
datePicker.updateDate(year, monthOfYear, dayOfMonth);
timePicker.setCurrentHour(currentHour);
timePicker.setCurrentMinute(currentMinute);
}

public void updateDate(int year, int monthOfYear, int dayOfMonth) {
datePicker.updateDate(year, monthOfYear, dayOfMonth);
}

public void setIs24HourView(final boolean is24HourView) {
timePicker.setIs24HourView(is24HourView);
}

public int getYear() {
return datePicker.getYear();
}

public int getMonth() {
return datePicker.getMonth();
}

public int getDayOfMonth() {
return datePicker.getDayOfMonth();
}

public int getCurrentHour() {
return timePicker.getCurrentHour();
}

public int getCurrentMinute() {
return timePicker.getCurrentMinute();
}

public boolean enableTime() {
return enableTimeCheckBox.isChecked();
}
}


나중에 좀더 속성을 추가하고, 살을 더 붙이면 쓸만할 것 같긴한데,
우선은 이것으로 대충 완성. 일단은 쓸 수는 있어보이니까.

다음은 간단하게 테스트.


테스트용 레이아웃 [res/layout/datetime_picker_test.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="wrap_content"
android:orientation="vertical">
<TextView android:id="@+id/datetime_picker_activity_text_view"
android:layout_width="fill_parent" android:layout_height="48dip"
android:textColor="#000000" android:background="#CCCCCC"
android:gravity="center" android:text="time"></TextView>
<com.babukuma.android.view.DateTimePicker
android:layout_width="wrap_content" android:layout_height="wrap_content"
year="2010" month="1" day="29" hour="14" minute="1" startYear="2000"
android:id="@+id/datetime_picker_activity_datetime_picker">
</com.babukuma.android.view.DateTimePicker>
</LinearLayout>


그리고 테스트용 소스 [DateTimePickerActivity.java]

package com.babukuma.android.test;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

import com.babukuma.android.view.DateTimePicker;
import com.babukuma.android.view.DateTimePicker.OnDateTimeChangedListener;

public class DateTimePickerActivity extends Activity {
final DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.datetime_picker_test);

final TextView timeView = (TextView) findViewById(R.id.datetime_picker_activity_text_view);
final DateTimePicker dateTimePicker = (DateTimePicker) findViewById(R.id.datetime_picker_activity_datetime_picker);
dateTimePicker
.setOnDateTimeChangedListener(new OnDateTimeChangedListener() {
@Override
public void onDateTimeChanged(DateTimePicker view,
int year, int monthOfYear, int dayOfMonth,
int hourOfDay, int minute) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, monthOfYear, dayOfMonth, hourOfDay,
minute);
timeView.setText(dateFormat.format(calendar.getTime()));
}
});

Calendar calendar = Calendar.getInstance();
calendar.set(dateTimePicker.getYear(), dateTimePicker.getMonth(),
dateTimePicker.getDayOfMonth(),
dateTimePicker.getCurrentHour(), dateTimePicker
.getCurrentMinute());
timeView.setText(dateFormat.format(calendar.getTime()));
}
}


다음에는 DateTimePickerDialog를 만들어봐야겠다.

2010年1月22日金曜日

[Android] ListView에 Button 추가

この投稿へのリンク
Android에서 빈번하게 사용하는 ListViewButton을 추가하는 방법.


우선, TextViewButton을 가지고 있는 레이아웃(리스트의 한행)을 작성한다.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content">
<TextView android:id="@+id/list_with_button_text"
android:layout_height="48dp" android:layout_width="fill_parent"
android:layout_marginRight="80dp"></TextView>
<Button android:id="@+id/list_with_button_button"
android:layout_alignParentRight="true" android:layout_height="48dp"
android:layout_width="80dp"></Button>
</RelativeLayout>

다음은 BaseAdapter를 상속받아서 Adapter를 작성한다.

package com.babukuma.android.test;

import static com.babukuma.android.test.Main.LOG_TAG;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

public class ListViewWithButton extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setListAdapter(new ListAdapterWithButton<String>(this, getResources()
.getStringArray(R.array.list_test_menus)));
}

class ListAdapterWithButton<T> extends BaseAdapter {
private final LayoutInflater mInflater;
private final T[] array;

public ListAdapterWithButton(final Context context, final T[] array) {
this.mInflater = LayoutInflater.from(context);
this.array = array;
}

@Override
public int getCount() {
return array.length;
}

@Override
public T getItem(int position) {
return array[position];
}

@Override
public long getItemId(int position) {
return position;
}

class ViewHolder {
TextView label;
Button button;
}

@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
ViewHolder holder;

if (convertView == null) {
convertView = mInflater
.inflate(R.layout.list_with_button, null);

// Creates a ViewHolder and store references to the two children
// views
// we want to bind data to.
holder = new ViewHolder();
holder.label = (TextView) convertView
.findViewById(R.id.list_with_button_text);
holder.label.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.d(LOG_TAG, "label click position=" + position);
}
});
holder.button = (Button) convertView
.findViewById(R.id.list_with_button_button);
holder.button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Log.d(LOG_TAG, "button click position=" + position);
}
});

holder.label.setText(array[position].toString());
holder.button.setText(array[position].toString());

convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

return convertView;
}

}
}


완성. 선택됐을 경우에 배경색을 바꾸는 건 필요하다면 수동으로 추가해야 할 듯.

[Android] ListViewにButtonを追加する。

この投稿へのリンク
Androidでよく使ってるListViewButtonを追加する方法。


まず、TextViewButtonを持つ行レイアウトを作る。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content">
<TextView android:id="@+id/list_with_button_text"
android:layout_height="48dp" android:layout_width="fill_parent"
android:layout_marginRight="80dp"></TextView>
<Button android:id="@+id/list_with_button_button"
android:layout_alignParentRight="true" android:layout_height="48dp"
android:layout_width="80dp"></Button>
</RelativeLayout>

次はAdapterをカスタマイズする。

package com.babukuma.android.test;

import static com.babukuma.android.test.Main.LOG_TAG;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

public class ListViewWithButton extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setListAdapter(new ListAdapterWithButton<String>(this, getResources()
.getStringArray(R.array.list_test_menus)));
}

class ListAdapterWithButton<T> extends BaseAdapter {
private final LayoutInflater mInflater;
private final T[] array;

public ListAdapterWithButton(final Context context, final T[] array) {
this.mInflater = LayoutInflater.from(context);
this.array = array;
}

@Override
public int getCount() {
return array.length;
}

@Override
public T getItem(int position) {
return array[position];
}

@Override
public long getItemId(int position) {
return position;
}

class ViewHolder {
TextView label;
Button button;
}

@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
ViewHolder holder;

if (convertView == null) {
convertView = mInflater
.inflate(R.layout.list_with_button, null);

// Creates a ViewHolder and store references to the two children
// views
// we want to bind data to.
holder = new ViewHolder();
holder.label = (TextView) convertView
.findViewById(R.id.list_with_button_text);
holder.label.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.d(LOG_TAG, "label click position=" + position);
}
});
holder.button = (Button) convertView
.findViewById(R.id.list_with_button_button);
holder.button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Log.d(LOG_TAG, "button click position=" + position);
}
});

holder.label.setText(array[position].toString());
holder.button.setText(array[position].toString());

convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

return convertView;
}

}
}


これで完了。

2010年1月8日金曜日

ANTでSVNを使用。

この投稿へのリンク
ANTで拡張タスク「<svn>」を使ってみる。

必要なライブラリは「svnant」。
http://subclipse.tigris.org/svnant.html サイトでダウンロードする。

まず、「<svn>」タスクを使って「export」のテスト結果。
$ ant -lib ./lib -buildfile svn_export.xml
Buildfile: svn_export.xml

main:
[echo] called target main
[input] remote repository url=
{リポジトリURL}
[input] local export dir=
{ローカル保存先}
[input] svn userid=
{svnのユーザID}
[input] svn password=
{svnのパスワード}

export:
[echo] SVN エクスポート。。。
[svn] <Export> started ...
[svn] <Export> finished.

BUILD SUCCESSFUL
Total time: 23 seconds


実行するときに「svnant」ライブラリが格納されてるPATHを知らせる為、
「-lib」オプションを付けるのが重要!


ソース(svn_export.xml)は
<?xml version="1.0" encoding="UTF-8"?>
<project default="main" basedir=".">
<typedef resource="org/tigris/subversion/svnant/svnantlib.xml" />

<!-- - - - - - - - - - - - - - - - - -
target: main
- - - - - - - - - - - - - - - - - -->
<target name="main">
<echo message="called target main" />
<input message="remote repository url=" addproperty="svn.repository" />
<input message="local export dir=" addproperty="svn.local" />
<input message="svn userid=" addproperty="svn.user" />
<input message="svn password=" addproperty="svn.password" />

<antcall target="export" />
</target>

<!-- - - - - - - - - - - - - - - - - -
target: export
- - - - - - - - - - - - - - - - - -->
<target name="export">
<echo>SVN エクスポート。。。</echo>
<svn username="${svn.user}" password="${svn.password}">
<export srcUrl="${svn.repository}" destPath="${svn.local}" />
</svn>
</target>
</project>


他のタスクや使い方は
http://subclipse.tigris.org/svnant/svn.html
を参照。

ANTでif文とfor文を使用。

この投稿へのリンク
ANTで拡張タスク「<if>」、「<for>」を使ってみる。

必要なライブラリは「ant-contrib」。
今回試してみたバージョンは「1.0b3」
ant-contrib サイトで「ant-contrib-1.0b3」をダウンロードする。

まず、「<if>」のテスト結果。
$ ant -buildfile if_test.xml
Buildfile: if_test.xml

main:
[echo] called target main
[echo] hello babukuma!
[echo] abcd else...

BUILD SUCCESSFUL
Total time: 0 seconds


ソース(if_test.xml)は
<?xml version="1.0" encoding="UTF-8"?>
<project default="main" basedir=".">
<!--<taskdef resource="net/sf/antcontrib/antlib.xml" />-->
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="lib/ant-contrib-1.0b3.jar"/>
</classpath>
</taskdef>

<!-- - - - - - - - - - - - - - - - - -
target: main
- - - - - - - - - - - - - - - - - -->
<target name="main">
<echo message="called target main" />
<property name="babu" value="kuma" />

<if>
<isset property="babu" />
<then>
<echo message="hello babukuma!" />
</then>
<else>
<echo message="babukuma else..." />
</else>
</if>

<if>
<isset property="abcd" />
<then>
<echo message="hello abcd!" />
</then>
<else>
<echo message="abcd else..." />
</else>
</if>
</target>
</project>


「<for>」のテスト結果。
$ ant -buildfile for_test.xml
Buildfile: for_test.xml

main:
[echo] called target main

test:
[echo] MSG:1

test:
[echo] MSG:2

test:
[echo] MSG:3

test:
[echo] MSG:4

BUILD SUCCESSFUL
Total time: 0 seconds


ソース(for_test.xml)は
<?xml version="1.0" encoding="UTF-8"?>
<project default="main" basedir=".">
<!--<taskdef resource="net/sf/antcontrib/antlib.xml" />-->
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="lib/ant-contrib-1.0b3.jar"/>
</classpath>
</taskdef>

<!-- - - - - - - - - - - - - - - - - -
target: main
- - - - - - - - - - - - - - - - - -->
<target name="main">
<echo message="called target main" />
<property name="msg.array" value="1,2,3,4" />

<for param="msg" list="${msg.array}">
<sequential>
<var name="msg" unset="true" />
<property name="msg" value="@{msg}" />
<antcall target="test" />
</sequential>
</for>
</target>

<!-- - - - - - - - - - - - - - - - - -
target: test
- - - - - - - - - - - - - - - - - -->
<target name="test">
<echo>MSG:${msg}</echo>
</target>
</project>


使ってみると結構便利だと俺は勝手に思った。(hiro_nemuさんには他のスクリプトでいいんじゃってつこまれたけど。)