使用 SimpleCursorAdapter 绑定到数据库的 ListActivity 中的行中各个元素的 OnclickListener 无法正常工作

发布于 2024-11-29 07:48:23 字数 2019 浏览 4 评论 0原文

请帮忙。

正如我在标题中所述,我试图使列表适配器的一行的各个元素根据用户单击的内容启动不同的操作。

它“有点”有效,但需要很长时间才能对用户点击做出反应。我做错了什么?

预先感谢,

所以我尝试了以下代码

    @Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    // Get the item that was clicked
    Cursor c = (Cursor) this.getListAdapter().getItem(position);

    // c.moveToNext();

    prescription_id = c.getString(0);
    TextView pName = (TextView) v.findViewById(R.id.text2);
    TextView paName = (TextView) v.findViewById(R.id.text3);
    TextView rDateLabel = (TextView) v.findViewById(R.id.textView1);
    TextView rDate = (TextView) v.findViewById(R.id.text4);
    TextView rLeftLabel = (TextView) v.findViewById(R.id.text5);
    TextView rLeft = (TextView) v.findViewById(R.id.text6);
    ImageView callPhone = (ImageView) v.findViewById(R.id.Call_Pharmacy);

    pName.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            goToPDetails();

        }
    });
    pa.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            goToPDetails();

        }
    });
    rDateLabel.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            goToPDetails();

        }
    });
    rDate.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            goToPDetails();

        }
    });
    rLeftLabel.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            goToPDetails();

        }
    });
    rLeft.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            goToPDetails();

        }
    });
    callPhone.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            //Some Code
        }
    });
}

Please help.

As I have stated in the title I am trying to make that individual elements of a row of a List adapter launch different actions depending on what the user click.

It "kind of" works but it takes LONG for it to react to user clicks. What is it that I am doing wrong?

Thanks in advance,

So I tried the following code in

    @Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    // Get the item that was clicked
    Cursor c = (Cursor) this.getListAdapter().getItem(position);

    // c.moveToNext();

    prescription_id = c.getString(0);
    TextView pName = (TextView) v.findViewById(R.id.text2);
    TextView paName = (TextView) v.findViewById(R.id.text3);
    TextView rDateLabel = (TextView) v.findViewById(R.id.textView1);
    TextView rDate = (TextView) v.findViewById(R.id.text4);
    TextView rLeftLabel = (TextView) v.findViewById(R.id.text5);
    TextView rLeft = (TextView) v.findViewById(R.id.text6);
    ImageView callPhone = (ImageView) v.findViewById(R.id.Call_Pharmacy);

    pName.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            goToPDetails();

        }
    });
    pa.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            goToPDetails();

        }
    });
    rDateLabel.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            goToPDetails();

        }
    });
    rDate.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            goToPDetails();

        }
    });
    rLeftLabel.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            goToPDetails();

        }
    });
    rLeft.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            goToPDetails();

        }
    });
    callPhone.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            //Some Code
        }
    });
}

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

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

发布评论

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

评论(2

隔纱相望 2024-12-06 07:48:23

所有这些 onClick 侦听器(一个 ListView 元素的单个子视图上的侦听器)可能不应该出现在 onListItemClick 方法中,而应该出现在 Adapter 的 getView 方法中(正确使用 ConvertView 参数)。

您这样做的方式似乎很错误,如果您在正确的位置正确实现了各种 onClick 侦听器,也许甚至不需要您的 onListItemClick 方法。

All those onClick listeners (those on single sub-views of one ListView element) probably shouldn't be here in the onListItemClick method, but in the getView method of your Adapter instead (with proper use of the convertView argument).

The way you do it seems quite wrong, maybe your onListItemClick method isn't even needed if you correctly implement the various onClick listeners at the right place.

ˇ宁静的妩媚 2024-12-06 07:48:23

为列表项使用基于 xml 的布局是这里的关键。使用两个属性 android:clickable="true"android:onClick="" 设置每个可单独点击的视图,该方法需要通过以下方式实现此签名: public void <您的点击处理程序> (View v) {...} 在您的活动中。附注是,您必须做出设计决策来实现一个点击处理程序来重叠处理(一个点击处理程序用于多个视图)或每个视图一个单一的视图处理程序,前者最适合点击基本上相似的情况在功能上与后者不同时。

下一步是实现点击处理程序,这里的关键是使用 ListView.getPositionForView(View v) 这样您就可以关联行、数据和单击的视图。

不要忘记实现 ListActivity.onListItemClick() 作为单击列表项的根布局的全部内容以及作为以下视图的全部内容没有自己的 onClick 处理程序集。

上述技术将具有良好的性能,并利用多个 Android API 来加速您的开发。

如果您决定在代码中实现侦听器,请仔细研究 getView() (如 darma 提到的),并且为了性能(如果您的列表中有多个项目)重用点击侦听器上面讨论了如何关联数据和行。

Using an xml based layout for your list item is key here. Set each individually clickable View with two attributes android:clickable="true" and android:onClick="<your click handler>" the method will need to be implemented with this signature: public void <your click handler> (View v) {...} in your Activity. A side note is that you'll have to make a design decision to implement a click handler to overlap handling (one click hanlder for more than one View) or a single view handler per View, the former is best for when click are substantially similar in function and the latter is when they are different.

The next step is to implement the click handler, the key here is to use ListView.getPositionForView(View v) so you can associate the row, the data, and the View clicked.

Don't forget to implement ListActivity.onListItemClick() as a catch-all for clicking on the root layout of the list item and as a catch-all for Views that don't have their own onClick handler set.

The above technique will have good performance and makes use of several Android API's to speed your development.

If you decide to implement the listeners in code, please study getView() closely (as darma mentioned) and for the sake of performance (if you have several items in your list) reuse the click listeners with the above discussion about how to associate the data and row.

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