2012-03-10 11 views
1

私は拡張可能なリストビューで作業しています。私はクリック可能な画像ビューを使用する必要があります。私は次のことを試みたが、うまくいっていない。それはヌルポインタ例外を与えます。画像リストでクリックリストを設定する

ここはmy xmlとlistviewのアダプターです。イメージビューにclickイベントを入れる必要があります。あなたがExpandableListActivityにそれを置くとエラーが発生します。

事前に感謝..

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);} 

そして、私のgroupview XMLは次のとおりです。

<?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> 

EDIT
今のNullPointerExceptionの問題が解決されます。しかし、私はonclicklistnerが動作していないという新たな問題を思いつきました。そのセクションに書かれたトーストやその他のものは実行されません。私はプログラムをデバッグし、imageviewがクリックされたときにコードがonclicklistnerに返されないことを発見しました。

+0

、あなたがする必要があります'Adapter'(ListViewのデータを表示するためのデータ)をあなたのコードからフィードしてください。私はそれを見ることができません。あなたが言及した画像ビューがリストビュー項目である場合、ListViewに対して単に 'onItemClick()'をオーバーライドすることができます。各項目に 'OnClickListener'を設定する必要はありません。 – neevek

+0

ここに 'main.xml'レイアウトを投稿してください。 –

答えて

1

あなたはこれを試すことができます、それがうまくいくはずです。ただ、ImageViewの中にあなたのxmlに2行を追加して、あなたの活動のクラスでボタンのクリックを聞くために関数を書く:

<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"/> 

そして、あなたの活動のクラスに:リストビューコントロールに対して

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

OK、ありがとう..これは動作していますが、なぜインラインclicklistnerが動作していないのか理解できません??? –

関連する問題