开发者

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

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.findViewBy开发者_运维知识库Id(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
        }
    });
}


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜