如何将onclick事件添加到addheaderView添加的listview中的按钮?

时间:2012-03-02 07:37:26

标签: android

我正在使用listview。在将主列表项添加到列表视图之前,我使用addheaderview(v)向列表视图添加标题。标题有两个按钮。

在logcat ..中给出了问题 btnhome.setOnClickListener(new View.OnClickListener(){

它强行关闭。 PLZ帮助我在下面的代码中出错。

等待你的回复 提前谢谢

enter code here

public void onCreate(Bundle 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);                    
          }
        });

3 个答案:

答案 0 :(得分:1)

只需从标题视图中获取按钮并设置onClickListener。

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

答案 1 :(得分: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>

这是你可以这样使用的例子。

答案 2 :(得分: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

            }
        });

    }