gpt4 book ai didi

java - 按钮在 ListView 中不起作用

转载 作者:行者123 更新时间:2023-12-01 13:32:19 25 4
gpt4 key购买 nike

我在 ListView 中有一个 Button
我想在单击另一个 Activity 时转到它。

但我收到此错误:
在范围内无法访问 Appointment 类型的封闭实例

@Override  
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub

View vi=convertView;

if(convertView==null)
vi = inflater.inflate(R.layout.item_row, null);

TextView txtsection = (TextView)vi.findViewById(R.id.section);
TextView txtdoctor = (TextView)vi.findViewById(R.id.doctor);
TextView txtdate = (TextView)vi.findViewById(R.id.date);
TextView txttime = (TextView)vi.findViewById(R.id.time);
btchange = (Button)vi.findViewById(R.id.change);

btchange.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(context, Available.class);
startActivity(i);

}
});

btdelete = (Button)vi.findViewById(R.id.delete);

txtsection.setText(item.section);
txtdoctor.setText(item.doctor);
txtdate.setText(item.date);
txttime.setText(item.time);

return vi; }}

最佳答案

在行

Intent i = new Intent(Appointment.this,Available.class);

Appointment.this 仅当适配器是 Appointment 的内部类时才有效。

如果不是这种情况,请使用传递给适配器的上下文。

Intent i = new Intent(context, Available.class);  

在适配器中声明一个名为 context 的私有(private)字段:

private Context context;  

在适配器构造函数中,分配传递给它的上下文:

this.context = context;  

将 getView 方法更改为:

@Override  
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub

if( convertView == null ){
convertView = inflater.inflate(R.layout.yourListLayout, parent, false);
}
Button btchange = (Button)convertView.findViewById(R.id.yourbuttonid);
btchange.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(context, Available.class);
startActivity(i);
}
});
return convertView;
}

编辑

btChange 还需要指向 playmaker420 所说的已点击的按钮。
我编辑了代码来实现这一点。

编辑2将您的代码适配器更改为:

package com.example.clinic; 

public class CustomListViewAdapter extends BaseAdapter
{
private Context context;
Button btchange,btdelete;
LayoutInflater inflater;
List<ListViewItem> items;

public CustomListViewAdapter(Activity context, List<ListViewItem> items) {
super();
this.context = context;
this.items = items;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return items.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub

View vi=convertView;

if(convertView==null)
vi = inflater.inflate(R.layout.item_row, null);

//add
ListViewItem item = items.get(position);

TextView txtsection = (TextView)vi.findViewById(R.id.section);
TextView txtdoctor = (TextView)vi.findViewById(R.id.doctor);
TextView txtdate = (TextView)vi.findViewById(R.id.date);
TextView txttime = (TextView)vi.findViewById(R.id.time);
btchange = (Button)vi.findViewById(R.id.change);

btchange.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(context, Available.class);

//updated
context.startActivity(i);

}
});

btdelete = (Button)vi.findViewById(R.id.delete);

txtsection.setText(item.section);
txtdoctor.setText(item.doctor);
txtdate.setText(item.date);
txttime.setText(item.time);

return vi;
}

关于java - 按钮在 ListView 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21496651/

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