我想在ListView中添加一個onItemClickListener
streamListView.setAdapter(this.streamAdapter);
this.streamListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Intent intent = new Intent(this, MessageDetailActivity.class);
startActivity(intent);
}
});
但是得到錯誤提醒:
The constructor Intent(new AdapterView.OnItemClickListener(){}, Class<MessageDetailActivity>) is undefined
如何解決這個問題呢?
在 annonymos 類裡 this 是 AdapterView.OnItemClickListener 類型的,用 YOURACTIVITYNAME.this 來指代activity
public class MyActivity extends Activity {
....
streamListView.setAdapter(this.streamAdapter);
this.streamListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
>>>>> Intent intent = new Intent(MyActivity.this, MessageDetailActivity.class);
startActivity(intent);
}
});
...
}