views:

254

answers:

1

Hi everybody,

I really need a help on this point because its the only thing not working in my app. I have a listview managed by an adapter that is extending BaseAdapter.

In the getview function im using the convertView to set up a OnClickListener. This OnClickListener work well when I am touching the screen but if I use the trackball of the HTC Desire, I see the row flashing on the screen, but the OnClickListener is not called...

I've read the android dev guide : http://developer.android.com/guide/topics/ui/ui-events.html and its say that I should receive a onClick event from the trackball...

Im putting here my getview code in case someone see anything strange. If you have any idea on what I can do, please let me know.

public View getView(int position, View convertView, ViewGroup parent) {
        //ici on va utiliser un mécanisme de cache avec le Tag Holder
        MyTag holder;           
        if (convertView == null){
            //On se branche sur le bon layout
            // convertView = inflater.inflate(android.R.layout.simple_list_item_2, null)
             convertView = inflater.inflate(R.layout.twolines, null);
            holder          = new MyTag();
            //on récupère les éléments du layout dans le holder
            holder.txtData  = (TextView)convertView.findViewById(R.id.productTitle);
            holder.txtExtra = (TextView)convertView.findViewById(android.R.id.text2);
            holder.imgScreenshot = (ImageView)convertView.findViewById(R.id.Screenshot);
            holder.imgFlag = (ImageView)convertView.findViewById(R.id.Flag);

            //holder.mLoginButton = (LoginButton) convertView.findViewById(R.id.login);
            holder.mPostButton = (ImageView) convertView.findViewById(R.id.postButton);

            //sauvegarde du holder
            convertView.setTag(holder);
        } else {
            holder = (MyTag)convertView.getTag();
        }


        //Facebook post button management
        holder.mPostButton.setOnClickListener((OnClickListener) new OnPostClickListener(convertView.getContext(),mFacebook, Long.toString(data[position].videoId), data[position].title, data[position].publisher, data[position].imageUrl));

        holder.mPostButton.setImageResource(com.cedemo.scan.utils.getPostButtonResId());


        //data update
        if(data[position].title != null)
            holder.txtData.setText(data[position].title);
        if(data[position].publisher != null)
            holder.txtExtra.setText(data[position].publisher);



        //get and set the language flag
        if(data[position].language != null)
            holder.imgFlag.setImageResource(com.cedemo.scan.utils.getFlag(data[position].language));

        //set the screenshot
        if(data[position].myVideoScreenshotBm != null)
            holder.imgScreenshot.setImageBitmap(data[position].myVideoScreenshotBm);


                    /* =================================================================

                       THIS FUNCTION IS NOT CALLED WHEN I CLICK ON THE TRACKBALL
                       BUT IS CALLED WHEN I TOUCH THE SCREEN.
                       WHEN I USE THE TRACKBALL THE SELECTED ROW IS FLASHING ONCE.

                       ================================================================= */
        //set the click mechanic if there is a valid video url
        if(data[position].url != null) {
            convertView.setOnClickListener((OnClickListener) new OnProductClickListener(position));


        }

        return convertView;
    }
+1  A: 

Maybe you want to use the recommended method setOnItemClickListener() on the ListView instead - I guess it is a bad style to set the listeners directly within the adapter.

mreichelt
Thanks you very much. Dont know why didnt try this solution before. Its i guess because I thought it was exactly the same thing. Therefor, i tested it and I dont understand why, but its work better. This time, the trackball is firing the event. I really wonder why there is a difference... If you are allowed to set up the click event in the convertview... why it would work differently then setting it up in the listview itself ?? Anyway. THANK YOU.
Fabien
I think you had some problems because of the trackball events were not getting through to the correct view. Your views of the adapter are children of the ListView, so the ListView might catch some events. Therefore these are not redirected to the child views.
mreichelt
It must be the reason. thanks.
Fabien