【问题标题】:buttons doesn't work inside a ListView按钮在 ListView 中不起作用
【发布时间】:2026-01-03 22:45:02
【问题描述】:

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

但我收到此错误:
No enclosing instance of the type Appointment is accessible in scope

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

【问题讨论】:

  • 你的适配器是activity的内部类吗?
  • 显示完整的适配器。可能你没有提供正确的上下文。
  • 您正在创建点击意图,但您从未启动过活动。
  • 不要编辑我的答案!发布 cmets 或编辑您的问题。
  • 对不起,以后不会再发生了

标签: java android android-listview android-button


【解决方案1】:

在线

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

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

如果不是这样,使用传递给适配器的上下文。

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

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

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

【讨论】:

  • 你能解释一下吗
  • 根据发布的代码,我无话可说。请发布更多代码。
  • 当我点击按钮“很遗憾,诊所已停止”时,它会给我一条弹出消息
  • 它给了我这个错误:这一行有多个标记 - 表达式的类型必须是数组类型,但它解析为 Class - 项目无法解析为类型 - 语法错误令牌“。”,此令牌之后的类
  • 它给了我这个“ListViewItem item = items.[position];”的错误但我将其更改为“ListViewItem item = items.get(position);”现在可以使用了,感谢您对我的耐心:)
【解决方案2】:
 Intent i = new Intent(Appointment.this,Available.class);
 startActivity(i);

【讨论】:

  • startActivity(i);没有什么不同
  • 您是否尝试单击列表视图行中的按钮? from ListViewItem item = items.get(position);如果是你将不得不从 item Button b= (Button)item.findViewById(R.id.yourbuttonid);现在调用按钮单击和意图
最近更新 更多