gpt4 book ai didi

android - 如何将 onclick 事件添加到通过添加标题 View 添加的 ListView 中的按钮?

转载 作者:行者123 更新时间:2023-11-30 04:19:07 25 4
gpt4 key购买 nike

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

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

然后强制关闭。请帮我看看下面代码哪里出错了。

等待您的回复提前谢谢

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);
}
});

最佳答案

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

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

关于android - 如何将 onclick 事件添加到通过添加标题 View 添加的 ListView 中的按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9529635/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com