ListView控件onItemClick不在活动工作 [英] ListView onItemClick not working in the Activity

查看:137
本文介绍了ListView控件onItemClick不在活动工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在里面有ListView中使用的活动这一点,也没有工作。

  usersList.setOnItemClickListener(新AdapterView.OnItemClickListener(){    公共无效onItemClick(适配器视图<>为arg0,ARG1观,诠释ARG2,
            翁ARG3){
    尝试{
        意向意图=新意图(UsersListActivity.this,
                UserActivity.class);
        startActivity(意向);
        }赶上(例外五){
            e.printStackTrace();
        }
    }
});

寻找一个解决方案,我结束了在的 getView()这件事以后在适配器

  convertView.setOnItemClickListener(新AdapterView.OnItemClickListener(){    公共无效onItemClick(适配器视图<>为arg0,ARG1观,诠释ARG2,
            翁ARG3){
    尝试{
        意向意图=新意图(mActivity,
                UserActivity.class);
        mActivity.startActivity(意向);
        }赶上(例外五){
            e.printStackTrace();
        }
    }
});

这工作。但是,为什么这样奇怪的行为。为什么没有它的工作应该的方式。

感谢您


解决方案

  

usersList.setOnItemClickListener(新
  AdapterView.OnItemClickListener(){


在这里,你已经使用查看适配器查看,这是父类的自定义适配器。这就是为什么它不工作。相反,你应该已经使用查看自定义适配器

这里

getView()功能可帮助您父 Adpater查看的视图转换为自定义适配器查看。

所以,您使用的两个选项,

  usersList.setOnItemClickListener(新CustomAdapterView.OnItemClickListener(){

  usersList.setOnItemClickListener(新OnItemClickListener(){

这两种选项可能会奏效。

When i am using this in the activity which has the ListView, it didn't work.

usersList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            ong arg3) {
    try {
        Intent intent = new Intent(UsersListActivity.this,
                UserActivity.class);
        startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
});

after searching for a solution i ended up doing it in the getView() of the Adapter

convertView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            ong arg3) {
    try {
        Intent intent = new Intent(mActivity,
                UserActivity.class);
        mActivity.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
});

This works. But why such strange behavior. Why isn't it working the way it should.

Thank You

解决方案

usersList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

Here you have used View of Adapter View, which is the parent class of your Custom Adapter. that's why it is not working. Instead you should have used the View of Custom Adapter.

getView() function here helps you to convert the View of Parent Adpater View to the Custom Adapter View.

So you use either of the two options,

usersList.setOnItemClickListener(new CustomAdapterView.OnItemClickListener() {

or

usersList.setOnItemClickListener(new OnItemClickListener() {

Both of the options might work.

这篇关于ListView控件onItemClick不在活动工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆