2011-01-02 7 views
0

지금 나는 listView에서 클릭 가능한 항목에 대한 솔루션을 찾고 있습니다. 클릭 가능 목록보기

는 우선이 건너 온 : developer.android.com/resources/articles/touch-mode.html 을하고 "정상적인"onListItemClick() behavouir이없는 것으로 나타났습니다. http://www.androidsnippets.org/snippets/125/

// LINE 296-321 

    @Override 
    protected ViewHolder createHolder(View v) { 
     // createHolder will be called only as long, as the ListView is not filled 
     // entirely. That is, where we gain our performance: 
     // We use the relatively costly findViewById() methods and 
     // bind the view's reference to the holder objects. 
     TextView text = (TextView) v.findViewById(R.id.listitem_text); 
     ImageView icon = (ImageView) v.findViewById(R.id.listitem_icon); 
     ViewHolder mvh = new MyViewHolder(text, icon); 

     // Additionally, we make some icons clickable 
     // Mind, that item becomes clickable, when adding a click listener (see API) 
     // so, it is not necessary to use the android:clickable attribute in XML 
     icon.setOnClickListener(new ClickableListAdapter.OnClickListener(mvh) { 

      public void onClick(View v, ViewHolder viewHolder) { 
       // we toggle the enabled state and also switch the icon 
       MyViewHolder mvh = (MyViewHolder) viewHolder; 
       MyData mo = (MyData) mvh.data; 
       mo.enable = !mo.enable; // toggle 
       ImageView icon = (ImageView) v; 
       icon.setImageBitmap( 
         mo.enable ? ClickableListItemActivity.this.mIconEnabled 
           : ClickableListItemActivity.this.mIconDisabled); 
      } 
     }); 

내가 매개 변수 보기 V을 발견 디버깅하는 동안이 텍스트 뷰 아닌 "정상"보기이며, 다음 물론 :

는 그럼 난에서 이 코드의 온 :

TextView text = (TextView) v.findViewById(R.id.listitem_text); 

returnes 널 (null) 그리고 NullPointerException이 발생합니다 ...

왜 그런가? 어떻게 해결할 수 있을까요?

미리 감사드립니다. :)

답변

1

ClickableListAdapter의 인스턴스를 어떻게 만듭니 까?

목록 어댑터를 만들 때 viewId 리소스 ID를 전달해야합니다.이 값은 나중에 layout이되어야합니다.

public ClickableListAdapter(Context context, int viewid, List objects) { 

     // Cache the LayoutInflate to avoid asking for a new one each time. 
     mInflater = LayoutInflater.from(context); 
     mDataObjects = objects; 
     mViewId = viewid; 

아래 코드는 생성자에 전달 된 XML 레이아웃을 팽창하고 createHolder를 호출합니다.

view = mInflater.inflate(mViewId, null); 
// call the user's implementation 
holder = createHolder(view); 

그래서 당신의 ClickableListAdapter를 인스턴스화 할 때, 당신은 통과 있는지 확인하는 대신 당신은 당신이 가지고있는 링크에서 가져온 다음과 XML 레이아웃을 생성해야 id

편집layout 제공됨 :

<?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" 
    android:gravity="center_vertical" 
    > 

<TextView android:text="Text" android:id="@+id/listitem_text" 
      android:layout_weight="1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      ></TextView> 
<ImageView android:id="@+id/listitem_icon" 
      android:src="@drawable/globe2_32x32" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:maxWidth="32px" 
      android:maxHeight="32px" 
      > 
</ImageView> 
</LinearLayout> 

레이아웃 디렉토리에 mylistrow.xml이라고 부르는 경우 R 어댑터로 : 여기

adapter = new MyClickableChannelListAdapter(this, R.layout.mylistrow, channelList); 
setListAdapter(adapter); 
+0

** 사실 내가 함께하고 있다고 생각 : ** 어댑터 = 새로운 MyClickableChannelListAdapter (이, \t \t \t \t android.R.layout.simple_list_item_1, channelList); \t \t setListAdapter (adapter); ** ** – Beasly

+0

아니면 내 자신의 (XML) 레이아웃을 얻기 위해 뭔가를해야합니까? – Beasly

+0

오 세상에 ... 문제를 발견 ... 아주 사소한 ... 여기 어댑터를 만드는 것은 잘못된 목록이었습니다 : dapter = new MyClickableChannelListAdapter (this, R.layout.mylistrow, channelList); setListAdapter (adapter); 나는 잘못된 XML을 사용했다는 것을 의미합니다. 도움을 주셔서 감사합니다 ... 저렇게 작은 것을 위해 시간을 찾고 있었는데 ... – Beasly

0

목록 항목은 즉시 상자에서 클릭 할 수 있어야합니다. ApiDemos 프로젝트 코드를보고 목록이 코딩 된 방식을 확인할 수 있습니다. 로컬 컴퓨터는 SDK의 일부이므로 로컬 컴퓨터에 있어야합니다. 나는 <root_sdk_folder>\platforms\android-2.0.1\samples\ApiDemos에있다.

+0

내가 전에을 writting되면서 ... : http://www.developer.android.com/resources/articles/touch-mode.html 의 ListView는 자아 그들에 의해 클릭 할 수 있습니다 터치 모드에서 ... 아직 해결책이 없습니다 ... Log.d ("TEST ...", "v.getClass() :"+ v.getClass(). toString()); 나를 보여줍니다 : TextView Log.d ("TEST ...", "v.getParent(). getClass() :"+ v.getClass().toString()); 표시 사항 없음 ...이 문제에 봉착했습니다. ( – Beasly

+0

@ 베 이스리 : http://developer.android.com/intl/zh-CN/resources/samples/ApiDemos/ 목록 작성 방법을 확인할 수도 있습니다. src/com/example/android/apis/view/index.html –

+0

방금 ​​전에 쓴 내용을 읽은 것이 유감입니까? 이전에 본 적이 있습니다 ... 여기에 게시하기 전에 해결책을 찾고있었습니다 ... 그리고 당연히 API를 통과했습니다. – Beasly

관련 문제