ListView の ArrayAdapter 内で定義したボタンから startActivity(intent); を実行したい

3,570 views
Skip to first unread message

Kou Sumeragi

unread,
Feb 1, 2011, 1:48:24 AM2/1/11
to android-...@googlegroups.com
水井と申します、初の質問となります。よろしくお願いします。
早速ですが、現在 ListView にカスタマイズした Adapter を登録し、
List のセル(各行)に TextView と Button 2個、の3つのアイテムを
登録し表示しております。

カスタマイズした Adapter 内の getView(int,View,ViewGroup) メソッドで

@Override
public View getView(int position,View convertView,ViewGroup parent) {
 TextView comment ;
 comment.setOnClickListener(new OnClickListener() {
  public void onClick(View v) {
  }
 });
 Button mButton = (Button)view.findViewById(R.id.listDelButton);
  public void onClick(View v) {
  }
 }) ;
 mButton = (Button)view.findViewById(R.id.listEditButton);
  public void onClick(View v) {
  }
 }) ;
 return view ;
}

といった形で各クリックイベントは検出できているのですが、この Adapter
の中から startActivity(intent) を実行できません。具体的には
「メソッド startActivity(Intent) は型 new View.OnClickListener(){} で未定義です」
と出てコンパイルが通らない状態です。

ちなみに、Intent intent = new Intent() は、呼び出し元 Activity の onCreate() で
Context a = this ; と定義し、Intent intent = new Intent(a, NextActivity.class );
として実装してあります。

Android や Java の経験や理解が浅いため、なぜ動かないかの原因がわからず、
お知恵を拝借したく。よろしくお願いします。

egg

unread,
Feb 1, 2011, 2:22:30 AM2/1/11
to android-...@googlegroups.com
水井さん

江川と申します。
startActivityがContextクラスのメソッドだからだと思います。
簡単な方法としては、ViewからContextをとる方法があります。
v.getContext().startActivity....
などです。


------------------------------------------
EGAWA Takashi

2011/2/1 Kou Sumeragi <kou...@gmail.com>:

> --
> このメールは Google グループのグループ「Android-SDK-Japan」の登録者に送られています。
> このグループに投稿するには、android-...@googlegroups.com にメールを送信してください。
> このグループから退会するには、android-sdk-ja...@googlegroups.com にメールを送信してください。
> 詳細については、http://groups.google.com/group/android-sdk-japan?hl=ja からこのグループにアクセスしてください。
>
>

Kou Sumeragi

unread,
Feb 1, 2011, 4:52:29 AM2/1/11
to android-...@googlegroups.com
江川さん

ありがとうございました、バッチリ行けました。

2011年2月1日16:22 egg <t.e...@gmail.com>:

Reply all
Reply to author
Forward
0 new messages