ListView 中的 OnItemClickListener() 不起作用

发布于 2024-10-26 15:44:29 字数 1546 浏览 7 评论 0原文

这是我用 SimpleCursorAdapter 填充 ListView 的代码。但我无法在此代码中实现 onClick 侦听器。实际上,代码运行正常,没有错误,但我在单击某个项目时没有得到任何输出...

public class Senditems extends Activity implements OnItemClickListener
{
    TextView output;
    DataHelper dh;
    ListView empListView;

    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sendto);

        Table1 employeeTable = new Table1(this);    
        employeeTable.open();
        Cursor c = employeeTable.fetchAllEmployee();
        if (c!= null)
        {
            SimpleCursorAdapter adapter2 = new SimpleCursorAdapter(this,
                R.layout.sendto,c, 
                new String[] {c.getColumnName(1),c.getColumnName(2)}, 
                new int[] {R.id.EmployeeName, R.id.EmployeeDesignation});
            empListView = (ListView)findViewById(R.id.Employee);
            empListView.setAdapter(adapter2);
            empListView.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view, int position, long id)                
                {
                    Toast.makeText(getApplicationContext(), "1 item clicked ",  Toast.LENGTH_SHORT).show();
                }
            });
       }

       employeeTable.close();
    }

    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) 
    {
        // TODO Auto-generated method stub      
    }
}

This is the code that I made to populate a ListView with a SimpleCursorAdapter. But I am not able to implement the onClick listener in this code. Actually the code is working properly without errors, but I am not getting any output on clicking an item...

public class Senditems extends Activity implements OnItemClickListener
{
    TextView output;
    DataHelper dh;
    ListView empListView;

    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sendto);

        Table1 employeeTable = new Table1(this);    
        employeeTable.open();
        Cursor c = employeeTable.fetchAllEmployee();
        if (c!= null)
        {
            SimpleCursorAdapter adapter2 = new SimpleCursorAdapter(this,
                R.layout.sendto,c, 
                new String[] {c.getColumnName(1),c.getColumnName(2)}, 
                new int[] {R.id.EmployeeName, R.id.EmployeeDesignation});
            empListView = (ListView)findViewById(R.id.Employee);
            empListView.setAdapter(adapter2);
            empListView.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view, int position, long id)                
                {
                    Toast.makeText(getApplicationContext(), "1 item clicked ",  Toast.LENGTH_SHORT).show();
                }
            });
       }

       employeeTable.close();
    }

    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) 
    {
        // TODO Auto-generated method stub      
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

远昼 2024-11-02 15:45:07

当实际视图由于行文件中的其他视图而未获得焦点时,就会发生这种情况,请检查我已经回答此处,可能是这很有帮助。

Its happens when actual view is not taking focus due to others view in your row file, check I already answered here, may be it's helps.

梦晓ヶ微光ヅ倾城 2024-11-02 15:45:06

我也面临同样的问题。我将其更改为 OnClickListner

rowView.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        //give ur code here
    }
});

这里的 rowView 对应于 ListView 中的行。

I also faced the same issue. I changed it to OnClickListner.

rowView.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        //give ur code here
    }
});

here rowView corresponds to the row within the ListView.

送君千里 2024-11-02 15:45:05

如果您正在实现 OnItemClickListener 并且 onItemClick 是自动生成的,您应该在其中编写操作:

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // TODO Auto-generated method stub
    if (view.equals(empListView))
    {
        Toast.makeText(getApplicationContext(),"1 item clicked ", Toast.LENGTH_SHORT).show();
    }  
}

并且还包括 empListView.setOnItemClickListener(this);

If you are implementing the OnItemClickListener and onItemClick is auto generated you should write the actions inside it:

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // TODO Auto-generated method stub
    if (view.equals(empListView))
    {
        Toast.makeText(getApplicationContext(),"1 item clicked ", Toast.LENGTH_SHORT).show();
    }  
}

and also include empListView.setOnItemClickListener(this);

你的往事 2024-11-02 15:45:04

您好,varada,我正在使用您的代码,我暗示您的代码在您的代码中所做的以下更改工作正常,

  1. 请在您的表中提供 _id 主键字段。
  2. 并使用此代码,它工作正常并给出吐司中的位置
    单击项目

    public class MainActivity 扩展 Activity 实现 OnItemClickListener
    {
        文本视图输出;
        列表视图 empListView;
    
        @SuppressWarnings(“弃用”)
        protected void onCreate(Bundle savingInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            SimpleCursorAdapter 适配器2=null;
            尝试 {
                Table1 员工表 = new Table1(this);    
                员工表.open();
                尝试 {
                     //employeeTable.insert("Ankit", "姓名");
                } catch (异常 e) {
                    // TODO: 处理异常
                    e.printStackTrace();
                }
    
    
    
                游标 c = employeeTable.fetchAllEmployee();
                if (c!= null)
                {
                     适配器2 = 新的SimpleCursorAdapter(这个,
                        R.layout.sendto,c, 
                        新的字符串[] {c.getColumnName(1),c.getColumnName(2)}, 
                        new int[] {R.id.EmployeeName, R.id.EmployeeDesignation});
    
               }
    
    
    
               员工表.close();
            } catch (异常 e) {
                // TODO: 处理异常
                e.printStackTrace();
    
            }
            empListView = (ListView)findViewById(R.id.Employee);
            empListView.setAdapter(适配器2);
            empListView.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterViewparent,View视图,int位置,长id)                
                {
                    Toast.makeText(getApplicationContext(), "position"+position, Toast.LENGTH_SHORT).show();
                }
            });
        }
    
        public void onItemClick(AdapterViewarg0, View arg1, int arg2, long arg3) 
        {
            // TODO 自动生成的方法存根      
        }
    }``
    

    3-sendto.xml

    <前><代码>

    <文本视图
    android:id="@+id/EmployeeDesignation"
    安卓:layout_width =“wrap_content”
    安卓:layout_height =“wrap_content”
    android:text="TextView" //>

Hi varada i am working with your code i impliment your code it is working fine the following change made in your code

  1. Please provide _id primary key field in yor table.
  2. and use this code it is working fine and give the position in toast
    on item click

    public class MainActivity extends Activity implements OnItemClickListener
    {
        TextView output;
        ListView empListView;
    
        @SuppressWarnings("deprecation")
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            SimpleCursorAdapter adapter2=null;
            try {
                Table1 employeeTable = new Table1(this);    
                employeeTable.open();
                try {
                     // employeeTable.insert("Ankit", "Name");
                } catch (Exception e) {
                    // TODO: handle exception
                    e.printStackTrace();
                }
    
    
    
                Cursor c = employeeTable.fetchAllEmployee();
                if (c!= null)
                {
                     adapter2 = new SimpleCursorAdapter(this,
                        R.layout.sendto,c, 
                        new String[] {c.getColumnName(1),c.getColumnName(2)}, 
                        new int[] {R.id.EmployeeName, R.id.EmployeeDesignation});
    
               }
    
    
    
               employeeTable.close();
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
    
            }
            empListView = (ListView)findViewById(R.id.Employee);
            empListView.setAdapter(adapter2);
            empListView.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view, int position, long id)                
                {
                    Toast.makeText(getApplicationContext(), "position"+position,  Toast.LENGTH_SHORT).show();
                }
            });
        }
    
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) 
        {
            // TODO Auto-generated method stub      
        }
    }``
    

    3-sendto.xml

    <TextView
        android:id="@+id/EmployeeName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
    
    <TextView
        android:id="@+id/EmployeeDesignation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
    

晚雾 2024-11-02 15:45:03

看起来,即使您使用 TextViewandroid:inputType 也可能会导致此问题。我遇到了同样的问题,并从 TextView 中删除属性 android:inputType 解决了问题。

Looks like android:inputType might lead to this problem, even if you use a TextView. I had the same issue and removing the attribute android:inputType from the TextViews fixed the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文