2011-01-06 3 views
0

gridview의 각 그림 아래에 텍스트를 삽입해야합니다. 누구든지 이걸 도와 줄 수 있니? 텍스트 뷰를 getView()에 넣으려고했지만 성공하지 못했습니다. 나는 이것을하기위한 나의 시도를 주석 처리했다.누구든지 gridview의 각 이미지 아래에 텍스트를 넣는 방법을 알고 있습니까?

이 내 이미지 어댑터 코드 :

import android.content.Context; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.GridView; 
import android.widget.ImageView; 
//import android.widget.TextView; 


public class ImageAdapter extends BaseAdapter{ 

    private Context mContext; 
    //private String[] texts = {"aaa", "bbb", "ccc", "ddd", "eee", "fff", "eee", "hhh", "iii"}; 

    public ImageAdapter(Context c) { 
     mContext = c; 
    } 

    public int getCount() { //Constructor 
     return getmThumbIds().length; //+9 
    } 

    public Object getItem(int position) { 
     return null; 
    } 

    public long getItemId(int position) { 
     return 0; 
    } 

    // create a new ImageView for each item referenced by the Adapter 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ImageView imageView; 
     //TextView tv; 
     if (convertView == null) { // if it's not recycled, initialize some attributes 
      imageView = new ImageView(mContext); 
      imageView.setLayoutParams(new GridView.LayoutParams(85, 85)); //Sets height and width for the view - Could use setAdjustViewBounds(boolean). 
      imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);//Image should be cropped towards centre 
      imageView.setPadding(8, 8, 8, 8); //Sets padding for all sides 
      //tv = new TextView(mContext); 
      //tv.setLayoutParams(new GridView.LayoutParams(85, 85)); 
     } else { 
      imageView = (ImageView) convertView; 
      //tv = (TextView) convertView; 
      //tv.setText(texts[position]); 
      //return tv; 
     } 

     imageView.setImageResource(getmThumbIds()[position]); //Chooses image from the array 
     return imageView; 

    } 

    public void setmThumbIds(Integer[] mThumbIds) { 
     this.mThumbIds = mThumbIds; 
    } 

    public Integer[] getmThumbIds() { 
     return mThumbIds; 
    } 

    // references to our images 
    Integer[] mThumbIds = { 
      R.drawable.puma_boots, R.drawable.cricket_balls, 
      R.drawable.darts, R.drawable.paintballing_gloves, 
      R.drawable.ice_skates, R.drawable.skate_ramp, 
      R.drawable.manu_top, R.drawable.rugby_ball, 
      R.drawable.puma_boots, R.drawable.cricket_balls, 
      R.drawable.darts, R.drawable.paintballing_gloves, 
      R.drawable.ice_skates, R.drawable.skate_ramp, 
      R.drawable.manu_top, R.drawable.tennis_racket, 
      R.drawable.puma_boots, R.drawable.cricket_balls, 
      R.drawable.darts, R.drawable.paintballing_gloves, 
      R.drawable.ice_skates, R.drawable.skate_ramp, 
      R.drawable.rugby_ball, R.drawable.tennis_racket 
    }; 


} 

답변

3

귀하의 getView() 필요가 LinearLayout (또는 RelativeLayout 또는 무엇이든)을 반환하는 ImageViewTextView을 포함.

+0

당신은 의미합니까, return 문 후, 그 안에 상대 레이아웃으로 XML을 참조? 이 코드를 작성하는 방법이 확실하지 않습니까? – Ben

+0

@Ben : http://commonsware.com/Android/excerpt.pdf와 같은 맞춤 어댑터를 만드는 방법을 설명하는 저서에서 발췌 한 내용입니다. ListView의 컨텍스트에서 설명하지만, 'GridView'에서도 잘 작동합니다. – CommonsWare

0

TextView 위에 CompoundDrawable을 사용할 수 있습니다. 이 같은

뭔가 :

public View getView(int position, View convertView, ViewGroup parent) { 
    TextView tv = (TextView)convertView; 
    if (tv == null) { 
     tv = new TextView(mContext); 
     tv.setLayoutParams(new GridView.LayoutParams(85, 85)); 
     // add more setup stuff here like padding, textColor, gravity, etc 
    } 

    tv.setCompoundDrawablesWithIntrinsicBounds(0, getmThumbIds()[position], 0, 0); 
    tv.setText(texts[position]); 
    return tv; 
} 
관련 문제