2012-03-10 2 views
1

확장 가능한 listview로 작업하고 있습니다. 클릭 가능한 이미지 뷰를 사용해야합니다. 나는 다음과 같은 노력했지만 작동하지 않았습니다. 그것은 널 포인터 예외를 제공합니다.이미지보기에서 클릭리스트를 설정하십시오.

여기 내 xml 및 listview 어댑터입니다. 이미지 뷰에 클릭 이벤트를 넣어야합니다. 정상적인 활동 onclicklistner에서 작동하지만, 당신이 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> 

편집
이제 NullPointerException이 문제가 해결된다. 하지만 onclicklistner가 작동하지 않는 새로운 문제가 발생했습니다. 토스트 또는 그 섹션에 쓰여진 내용은 실행되지 않습니다. 프로그램을 디버그하고 imageview가 클릭 될 때 코드가 onclicklistner에 반환되지 않는 것을 발견했습니다.

어떤 아이디어 ??

+0

, 당신은에 있습니다 'Adapter' (ListView를 표시하기위한 데이터)를 피드에 코드에서 볼 수 없습니다. 당신이 언급 한 이미지 뷰가리스트 뷰 아이템이라면 ListView에 대해'onItemClick()'을 오버라이드 할 수 있습니다. 각 아이템에 대해'OnClickListener'를 설정할 필요는 없습니다. – neevek

+0

'main.xml' 레이아웃을 여기에 게시하십시오. –

답변

1

시도해 볼 수 있습니다. 제대로 작동해야합니다. 그냥 이미지 뷰 안에 당신의 XML에 두 줄을 추가 한 다음 활동 클래스의 버튼 클릭을 듣고 함수를 작성 :

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

그리고 활동 클래스 : ListView를 들어

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

좋아, 고마워 ..이 일하고 ..하지만 캔트는 인라인 clicklistner가 작동하지 않는 이유를 이해 ??? –

관련 문제