2012-03-02 184 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