1

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
        }
    });
}
monn3t
  • 675
  • 1
  • 6
  • 22
  • Are you testing this on a real device or the AVD? – Jack Aug 11 '11 at 23:00
  • I want to make sure I understand, are you tried to make every view individually clickable or just the whole row? – Dan S Aug 11 '11 at 23:01
  • @Jack I have test it on the ADV so far. I understand it can be slower but I believe it is taker longer than it should. – monn3t Aug 12 '11 at 22:29
  • @Dan What you described is exactly what I want to do. I am doing it this way (at least for the text, because it is in a relative layout and I could not fit the imageView outside of it. – monn3t Aug 12 '11 at 22:30

2 Answers2

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.

darma
  • 4,687
  • 1
  • 24
  • 25
  • I had faced a problem similar to the OP, where I overrode `ArrayAdapter` to return a custom row in `getView()`. As @darma mentioned above, when I did this, the `OnItemClickListener()` wouldn't get invoked at all. I solved this by setting `OnClickListener` in my `getView()` method instead. Works fine for me. I'm not aware of the performance implications though! – curioustechizen Aug 12 '11 at 02:17
  • When you set a "listener", it then "listens" for a given kind of event as long as it's there, so it should only be created once for that given element and given kind of event. Here you create and set ONE MORE TIME multiple "onClick" listeners on children each time you click their parent element, which no doubt causes a lot of trouble and performance issues. I'm even surprised it "kind of" works like this! – darma Aug 12 '11 at 11:19
  • Well actually, I removed the `OnItemClickListener()` and instead use only the `OnClickListener` in `getView()`. I realize this is not the best way of doing things - it is just a stop-gap until I figure out why the OnItemClickListener is not being called at all in my case. – curioustechizen Aug 12 '11 at 14:12
  • THANK YOU DARMA: I tried your suggestion, after trying Dan's approach first. But it really paid off. Thank you for answering my question. It is very much appreciated :-D. P.S.: I ran into another problems along the way, but I solve them as well, that is why I didn't thank you before. – monn3t Aug 14 '11 at 01:50
1

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.

Dan S
  • 9,139
  • 3
  • 37
  • 48
  • THANK YOU so very much for your answer. It is the way I would like my situation solved. Unfortunately the target version for my app is 3 or 1.5 and what you suggested is only available from the next version on. I tried it and after I while I saw the small letters in the documentation that states it is not supported in my case. :-D I worked a little bit harder and I went Darma's Way. THANK YOU IMMENSELY THOUGH!!! – monn3t Aug 14 '11 at 01:46