英文:
How to set listener correctly?
问题
如果在适配器视图中初始化了onItemClickListener,那么如何正确地为列表视图设置setOnItemClickListener?我有以下代码,并且在倒数第二行和倒数第一行字符串上出现了错误,我在那里尝试了listView.setOnClickListener(itemClickListener);
。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_top_level);
AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> listenView, View itemView, int position, long id) {
if (position == 0) {
Intent intent = new Intent(TopLevelActivity.this, DrinkCategoryActivity.class);
startActivity(intent);
}
}
};
ListView listView = (ListView) findViewById(R.id.list_options);
listView.setOnItemClickListener(itemClickListener);
}
英文:
If there is an initializing of onItemClickListener with adapter view, how can I set setOnItemClickListener for list view correctly? I have following code and I have a mistake on the before and before last one string, where I try to listView.setOnClickListener(itemClickListener);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_top_level);
AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> listenView, View itemView, int position, long id) {
if (position == 0) {
Intent intent = new Intent(TopLevelActivity.this, DrinkCategoryActivity.class);
startActivity(intent);
}
}
};
ListView listView = (ListView) findViewById(R.id.list_options);
listView.setOnClickListener(itemClickListener);
}}
答案1
得分: 0
为什么不尝试使用以下代码替代itemclicklistener:--
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
// TODO Auto-generated method stub
Intent i = new Intent(getApplicationContext(), destinationclass.class);
startActivity(i);
}
});
英文:
why not try this instead of itemclicklistener:--
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
// TODO Auto-generated method stub
Intent i = new Intent(getApplicationContext(), destinationclass.class);
startActivity(i);
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论