如何正确设置监听器?

huangapple go评论78阅读模式
英文:

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&lt;?&gt; 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&lt;?&gt; adapterView, View view, int position, long l) {
            // TODO Auto-generated method stub
            Intent i = new Intent(getApplicationContext(), destinationclass.class);
            startActivity(i);

        }
    });

huangapple
  • 本文由 发表于 2020年9月7日 18:33:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63775856.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定