establecer lista de clics en la vista de imagen

I'm working with expandable listview. In that I need to use click-able imageview. I've tried following but not working. It gives null pointer exception.

here is my xml and listview adapter. I need to put click event on the imageview. from normal activity onclicklistner works perfact, but gives error if you put that in ExpandableListActivity.

Gracias de antemano ..

public class ExpList extends ExpandableListActivity
{
//    static final String parent_node[] = { "grey", "blue", "yellow", "red" };

String parent_node[];

static final String shades[][] = {
  {
    "lightgrey","#D3D3D3",
    "dimgray","#696969",
    "sgi gray 92","#EAEAEA"
  }, {},
  {
    "dodgerblue 2","#1C86EE",
    "steelblue 2","#5CACEE",
    "powderblue","#B0E0E6"
  }, {}
};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle)
{
    super.onCreate(icicle);
    setContentView(R.layout.main);   


    LayoutInflater group_inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
     View group_layout = group_inflater.inflate(R.layout.group_row, null);

    ImageView img_call = (ImageView)group_layout.findViewById(R.id.imgcall);

    img_call.setOnClickListener(new OnClickListener() 
    {           
  @Override
  public void onClick(View v) 
      {
        Toast.makeText(getBaseContext(), "clicked...", Toast.LENGTH_SHORT).show();
     }); 

SimpleExpandableListAdapterWithEmptyGroups expListAdapter =
            new SimpleExpandableListAdapterWithEmptyGroups(
                this,
                createGroupList(),                      
                R.layout.group_row,                     
                new String[] { "colorName" },           
                new int[] { R.id.groupname },           
                createChildList(),                      
                R.layout.child_row,                     
                new String[] { "shadeName", "rgb" },    
                new int[] { R.id.childname, R.id.rgb }  
            );
        setListAdapter( expListAdapter );}

And my groupview xml is:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

<ImageView 
    android:src="@drawable/expander_group"
    android:id="@+id/explist_indicator"
    android:layout_width="35px"
    android:layout_height="35px"/>


<ImageView 
    android:src="@drawable/contact_thumbnail"
    android:id="@+id/img_contact_thumbnail"
    android:layout_width="50px"
    android:layout_height="50px"/>

<TextView
    android:id="@+id/groupname"
    android:layout_width="190px"
    android:layout_height="match_parent"
    android:paddingLeft="30px"
    android:textSize="16px"
    android:textStyle="bold" />

<ImageView 
    android:id="@+id/imgcall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/call"
    android:clickable="true" />
</LinearLayout>

EDITAR
Now NullPointerException problem is solved. But I came up new problem that onclicklistner is not working. No Toast or anything else written in that section doesn't execute. I debug program and found that code does not return on onclicklistner when imageview get clicked.

¿¿Alguna idea??

preguntado el 10 de marzo de 12 a las 08:03

For a ListView, you have to feed it with an Adapter(the data for the ListView to display), from your code I can't see one. If the imageview you mentioned is the listview item, you may simply override onItemClick() for the ListView, you don't need to set OnClickListener para cada artículo. -

Publica tu main.xml layout here. -

1 Respuestas

You can try this, hope fully it should work. Just add two line in your xml inside imageview and then write a function to listen the button click in your activity class:

<ImageView 
    android:id="@+id/imgcall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/call"
    android:clickable="true" 
    android:onClick="onClickHandler"/>

And in your activity class :

public void onClickHandler (View v) {
    switch (v.getId()) {
        case R.id.imgcall:
            // do what ever you want here ...
            break;
    }
}

respondido 10 mar '12, 09:03

ok, thank you.. this is working.. but cant understand why inline clicklistner is not working??? - Nunca renunciar

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.