2014-02-01 2 views
0

У меня есть Button внутри ListView.
Я хочу перейти к другому Activity при нажатии.кнопки не работают внутри ListView

Но я получаю эту ошибку:
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; }} 
+0

ли вы переходник внутреннего класса активности? – Raghunandan

+0

показать полный переходник. Вероятно, u не обеспечивает правильный контекст. – keshav

+1

Вы создаете намерение щелкнуть, но вы никогда не начинаете действие. – Ayoub

ответ

1

В строке

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

Appointment.this действует только тогда, когда adaptar является внутренним классом Назначения.

Если это не так, используйте Контекст, переданный адаптеру.

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

Объявите частное поле, названный контекст, в адаптере:

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

EDIT

btChange также необходимо указать кнопку, нажав кнопку playmaker420.
Я отредактировал код для этого.

EDIT 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; 
} 
+0

Можете ли вы объяснить больше, пожалуйста, – user3124414

+0

Основываясь на кодексе, я ничего не могу сказать. Пожалуйста, напишите больше кода. – ramaral

+0

он дает мне поп-сообщение, когда я нажимаю кнопку, говорит «к сожалению, клиника остановилась» – user3124414

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

startActivity (i); не делает другой – user3124414

+1

Вы пытаетесь нажать кнопку в строке списка? из списка ListViewItem item = items.get (position); Если да, вам нужно будет найти представление кнопки из элемента Button b = (Button) item.findViewById (R.id.yourbuttonid); Теперь позвоните на кнопку и намерение – playmaker420

+0

да, что я хочу – user3124414

Смежные вопросы