2012-03-02 15 views
1

私はリストビューを使用しています。メインリスト項目をリストビューに追加する前に、addheaderview(v)を使用してリストビューにヘッダーを追加します。ヘッダーには2つのボタンがあります。 logcat..itでaddheaderViewによって追加されたlistviewのボタンにonclickイベントを追加する方法は?

は btnhome.setOnClickListener(新View.OnClickListener(){

に問題を与え、それが強制終了。 エラーは以下のコードである場合plzは私を助けて。

を待っています事前にご返信 ありがとう

enter code here 

ます。public voidのonCreate(バンドルsavedInstanceState){

 super.onCreate(savedInstanceState); 
     setContentView(R.layout.games_by_category); 

     Global globalclass=((Global)getApplicationContext()); 
     globalclass.setpageno(0);     
     lstview =(ListView)findViewById(android.R.id.list);  

     LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);    
     View v = inflater.inflate(R.layout.header, null); 
     //View v = getLayoutInflater().inflate(R.layout.header, null); 
     v.setMinimumHeight(10); 
     lstview.addHeaderView(v); 

     btnhome=(Button)findViewById(R.id.btnHome); 
     btnhome.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) 
      { 
       btnhome.setTextColor(Color.BLACK); 
       Global globalclass=((Global)getApplicationContext()); 
       Intent inte=new Intent(GamesByCatActivity.this,HotGamesActivity.class); 
       startActivity(inte);      
      } 
     }); 

答えて

0

あなたの問題を理解できません。何をしたいですか?あなたはListViewとヘッダを持っているなら、あなたはLinearLayoutを使用します。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/_lv_babies" > 
    </ListView> 
      <RelativeLayout android:layout_width="fill_parent" 
     android:layout_height="wrap_content" android:background="@color/blue_email" 
     > 
     <Button android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Edit" android:textSize="15dp" android:padding="10dp" 
      android:layout_margin="5dp" android:textColor="@color/black" 
      android:id="@+id/_btn_edit"/> 
     <Button android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Add" android:textSize="15dp" android:padding="10dp" 
      android:layout_margin="5dp" android:textColor="@color/black" 
      android:id="@+id/_btn_add"/> 

</RelativeLayout> 
</LinearLayout> 

これは、あなたがこのように使用できる例です。

+0

返信用HIIのTHX ..私は私のリストにaddheaderviewを追加しました。そのヘッダーhavボタン。私はこれらのボタンでonclickイベントをしたい。これどうやってするの。 – ZooZoo

1

ヘッダービューからボタンを取得し、onClickListenerを設定するだけです。

View header; 
Button headerButton = header.findViewById(R.id.btn1); 
headerButton.setOnClickListener(this); 
listView.addHeaderView(header); 
+0

私はbtが動作しないようにしました:( – ZooZoo

+0

urヘルプのおかげで – ZooZoo

0
For your button click....if its works then tick on right click or accept my answer. 
protected void setXmlComponents(){ 
     _btn_add =(Button)findViewById(R.id._btn_add); 
     _btn_edit =(Button)findViewById(R.id._btn_edit); 
      } 
    protected void setListener(){ 
     _btn_add.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 
          } 
     }); 
     _btn_edit.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 

      } 
     }); 

    } 
+0

これは私の問題の解決策ではありません:( – ZooZoo

+0

これをあなたのxmlのリストビューに含めてください下記のコードを編集してくださいbtnhome = (ボタン)findViewById(R.id.btnHome); btnhome.setOnClickListener(新View.OnClickListener(){ ます。public void onClickの(ビューV) { } }); – androiddeveloper2011

+0

私はこれを追加しましたが、まだそれが強制終了.. :( – ZooZoo

関連する問題