2012-03-02 188 views
1

我使用的是一個listview。在將主要列表項添加到列表視圖之前,我使用addheaderview(v)向列表視圖添加了一個標題。標題有兩個按鈕。如何將addclick事件添加到listview中的按鈕添加addheaderView?

在logcat..it在 btnhome.setOnClickListener(新View.OnClickListener(){

給人的問題,它強制關閉。 plz幫助我在哪裏,在下面的代碼錯誤。

等待您的回覆 thanx提前

enter code here 

公共無效的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到我的清單。 ñ頭部有按鈕。我想在這些按鈕上點擊事件。我怎樣才能做到這一點。 – ZooZoo 2012-03-02 07:59:28

1

只需從標題視圖中獲取按鈕並設置onClickListener。

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

我想BT不工作:( – ZooZoo 2012-03-02 09:55:46

+0

感謝烏拉圭回合的幫助 – ZooZoo 2012-03-02 11:03:02

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 2012-03-02 09:56:17

+0

包括這在你的XML下面的ListView <包括佈局=「@佈局/頭」 />編輯下面btnhome =給出你的代碼(按鈕)findViewById(R.id.btnHome); btnhome.setOnClickListener(新View.OnClickListener(){ 公共無效的onClick(視圖v){ } }); – androiddeveloper2011 2012-03-02 10:05:34

+0

我將此但仍強制關閉.. :( – ZooZoo 2012-03-02 10:06:48