2013-02-28 1 views
2

SimpleCursorAdapter로 채우는 ListView가 있습니다. 내 목록의 각 항목에 대해 TextView> RatingBar> TextView> ImageView 순으로 정렬됩니다. 나는 이미지 뷰에 버튼을 추가 할 수 있지만이에 ... SimpleCursorAdapter 내의 버튼

가 어디에서리스트 뷰를 채우는 방법을 잘 모릅니다 :

adapter = new SimpleCursorAdapter(this, R.layout.beerdrunk, cursor, 
      new String[] { "beers", "notes", "numbers" }, new int[] { 
        R.id.champName, R.id.champNote, R.id.champDrunk }); 
    adapter.setViewBinder(binder); 
    list = (ListView) findViewById(R.id.listMyBeers); 
    list.setItemsCanFocus(true); 
    list.setOnItemClickListener(onClickBeer); 
    list.setAdapter(adapter); 

OnItemClickListener :

private OnItemClickListener onClickBeer = new OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
      long arg3) { 
     System.out.println(""+ arg0); 
     System.out.println(""+ arg1); 
     System.out.println(""+arg2); 
     Intent newActivity = new Intent(MyBeers.this, DetailsBeer.class); 
     newActivity.putExtra("com.example.checkmybeer.DetailsBeer", "" 
       + arg3); 
     startActivityForResult(newActivity, 0); 
    } 
}; 

그리고 XML에 대한 맥주에 취한는 :

,691 :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"> 

<TextView 
    android:id="@+id/champName" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="130" 
    android:gravity="center_vertical" 
    android:layout_gravity="center_vertical" 
    android:maxLines="3" 
    android:padding="5dp" 
    android:paddingRight="20dp" 
    android:singleLine="false" 
    android:text="@string/beerBook" 
    android:textColor="@color/black1" 
    android:textSize="20sp" 
    android:textStyle="bold" > 
</TextView> 

<View 
    android:layout_width="1.5dip" 
    android:layout_height="fill_parent" /> 

<RatingBar 
    android:id="@+id/champNote" 
    style="@style/beerRatingSmall" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:isIndicator="true"   
    android:numStars="5" 
    android:layout_margin="1.5dp" 
    android:rating="5" 
    android:stepSize="0.10" > 
</RatingBar> 

<View 
    android:layout_width="1.5dip" 
    android:layout_height="fill_parent" /> 

<TextView 
    android:id="@+id/champDrunk" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:layout_weight="30" 
    android:gravity="right|center_vertical" 
    android:padding="5dp" 
    android:layout_marginLeft="5dp" 
    android:text="200" 
    android:textColor="@color/black1" 
    android:textSize="20sp" 
    android:textStyle="bold" /> 

<View 
    android:layout_width="1.5dip" 
    android:layout_height="fill_parent" /> 

<ImageView 
    android:id="@+id/champPlusone" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_vertical|right" 
    android:layout_margin="5dp" 
    android:layout_weight="30" 
    android:gravity="right|center_vertical" 
    android:clickable="false" 
    android:src="@drawable/plus2" /> 
</LinearLayout> 

내가 좋아하는 뭔가를 시도했다

if(arg2 instanceof ImageView){ 
    //do some stuff 
    } 

하지만 작동하지 않습니다 ... 어떻게하면 내 "행"에서 어떤보기를 클릭했는지 알 수 있습니까?

+0

"내보기에서 어떤보기를 클릭했는지 어떻게 알 수 있습니까?" 사용자 지정 어댑터를 만들고 각 뷰에서 특별한 작업을 수행 할 OnClickListeners를 설정해야합니다. – Sam

+0

빠른 질문, 왜 목록보기에 단추를 추가 하시겠습니까? 반대로 사용자가 행 자체를 클릭하는 것과 반대? –

+0

@ BrentHronik 나는 저자가 RatingsBar가 한 가지 일을하고, Button이 다른 일을하고, 행 자체가 뭔가 다른 일을하려고한다고 가정합니다. – Sam

답변

2

이미지를 클릭하려면 ImageView 대신 ImageButton을 사용하십시오.
Activity의 메소드를 호출하는 버튼에 onClick 속성을 설정할 수 있습니다.

SimpleCursorAdapter을 확장하고 bindView 메서드에서 각 버튼의 수신기를 onClick 개 추가하십시오. (어쩌면 public void buttonClicked(int position, View v)... 그래서 당신이보기를 클릭 한 행과 알고 ... 그것은 당신의 활동에 메소드를 호출하게 여기

당신이 사용할 수 CursorAdapter의 예를 구현 한 것입니다

class CustomCursorAdapter extends CursorAdapter{ 
    LayoutInflater inflater; 

    public CustomCursorAdapter(Context context, Cursor c, int flags){ 
     super(context,c,flags); 
     inflater = LayoutInflater.from(context); 
    } 

    @Override 
    public void bindView(View view, Context context, Cursor cursor){ 
     int row_id = cursor.get('_id'); //Your row id (might need to replace) 
     ImageButton button = (ImageButton) view.findViewById(R.id.champPlusone); 
     button.setOnClickListener(new OnClickListener(){ 
      @Override 
      public void onClick(View v){ 
       //ADD STUFF HERE you know which row is clicked. and which button 
      } 
     }); 
    } 

    @Override 
    public View newView(Context context, Cursor cursor, ViewGroup parent){ 
     LayoutInflater inflater = LayoutInflater.from(context); 
     View v = inflater.inflate(R.layout.beerdrunk, parent, false); 
     bindView(v,context,cursor); 
     return v; 
    } 

} 

을,..)

+1

이 샘의 코멘트는 제게 많은 도움이됩니다 !! :) – user2121458