OnclickListener for individual elements in a row from a ListActivity using SimpleCursorAdapter to Bind to DB not working properly

不羁的心 提交于 2020-01-14 03:28:11

问题


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
        }
    });
}

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/7033858/onclicklistener-for-individual-elements-in-a-row-from-a-listactivity-using-simpl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!