按钮在ListView中不起作用

时间:2014-02-01 10:05:53

标签: java android android-listview android-button

Button内有ListView 我想点击它时转到另一个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;  }}

2 个答案:

答案 0 :(得分:1)

在第

Intent i = new Intent(Appointment.this,Available.class);
仅当adaptar是Appointment的内部类时,

Appointment.this才有效。

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

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

答案 1 :(得分:0)

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