George
George

Reputation: 5681

click an imageview in a listview works if i do it inside my custom adapter but not in main activity

I have a listview which has an image.I want to make an action when the image is clicked. Doing that from my adapter , works ,but I want to do it from my mainactivity where the listview lies.

I have my custom adapter:

ImageView myImage=(ImageView) convertView.findViewById(R.id.myimage);
            byte []temp = theItems.getImagemyItems();
            Bitmap image = BitmapFactory.decodeByteArray(temp, 0, temp.length);
            myImage.setImageBitmap(image);

            myImage.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Toast.makeText(context, "CLICKED!", Toast.LENGTH_SHORT).show();      


                }
            });

The above works fine. If I try from mainactivity:

View inflatedView = getLayoutInflater().inflate(R.layout.showadapterlist, null);
this.imageView = (ImageView) inflatedView.findViewById(R.id.myimage);   

 imageView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Toast.makeText(getBaseContext(), "CLICKED!", Toast.LENGTH_SHORT).show();      

                }

            }); 

It doesn't work.Nothing happens.

Upvotes: 0

Views: 1852

Answers (3)

Leonardo Sapuy
Leonardo Sapuy

Reputation: 2740

Try using the onTouch event, or change the image for a imagebutton...

But it's better if you do it with the main onClick method()

@Override
public void onClick(final View v) {
         switch(v.getId()){
             case R.id.myimage:
                  //Do it when myimage is clicked
                  Toast.makeText(class.this, "CLICKED!",Toast.LENGTH_SHORT).show();                   
             break;
             case R.id.otherID:
                  //Do something when other item is clicked
             break;
          }

}

And say goodbye to the onClick listeners...

Other options is setting the property onclick in the layout... An example:

 <ImageView
      android:id="@+id/myimage"
      android:layout_width="170dp"
      android:layout_height="115dp"
      android:onClick="yourmethodname"
      android:scaleType="centerCrop"
      android:src="@drawable/iconcamera3" />

And in the java class add:

public void yourmethodname(final View v) {
     Toast.makeText(class.this, "CLICKED!",Toast.LENGTH_SHORT).show();   
     //Do anything what you want to do on the onClick event

}

I don't like to use the onclick listeners, so I use the methods and never has failed.. try it...

EDIT: For use the first way, overriding the onClick method, first we have to implement onClickListener:

import android.view.View.OnClickListener;
public class AnyClass extends Activity implements OnClickListener{
@Override
public void onClick(final View v) {
         switch(v.getId()){
             case R.id.myimage:
                  //Do it when myimage is clicked
                  Toast.makeText(class.this, "CLICKED!",Toast.LENGTH_SHORT).show();                   
             break;
             case R.id.otherID:
                  //Do something when other item is clicked
             break;
          }

}
}

Upvotes: 2

Serj Lotutovici
Serj Lotutovici

Reputation: 4380

You dont have to set an OnClickListener to the ListView object. You can pass the click listener to you adapter when creting it. Try something like this:

class YourActivity extends Activity {
    protected onCreate(Bundle saveInstaceState) {
       super.onCreate(saveInstaceState);
       // some code here
       YourAdapter adapter = new Adapter(this, clickListener);
       listview.setAdapter(adapter);
       // more code here
   }
}

and your class adapter should have a constructor that saves this listener:

public YourAdapter {
    public YourAdapter(Context context, OnClickListener listener) {
      // set everithing you need
      this.listener = listener;
    }

    public View getView (...) {
    // do everithig you need
    myImage.setOnClickListener(listener);
    }
}

Upvotes: 2

Alex Orlov
Alex Orlov

Reputation: 18107

Try adding this attributes to your imageview:

android:focusable="false"

android:focusableInTouchMode="false"

android:clickable="false"

Upvotes: 0

Related Questions