2014-03-05 128 views
0

enter image description here 我正在为应用程序与网格视图的android。我在图像下使用了文本视图的网格视图代码。但文本视图不显示。 Eclipse中没有显示我的logcat文本视图不显示在网格视图与图像

public class ImageAdapter extends BaseAdapter { 
    private Context mContext; 
    TextView text; 
    private String[] mThumbTxt = { 
      "Example Text 1", "Example Text 2", "Example Text 3", " Example Text 4","Example Text 5", 
      "Example Text 1", "Example Text 2", "Example Text 3", " Example Text 4","Example Text 5", 
      "Example Text 1", "Example Text 2", "Example Text 3", " Example Text 4","Example Text 5", 
      "Example Text 1", "Example Text 2", "Example Text 3", " Example Text 4","Example Text 5", 
      "Example Text 1", "Example Text 2", "Example Text 3", " Example Text 4","Example Text 5", 
      "Example Text 1", "Example Text 2", "Example Text 3", " Example Text 4","Example Text 5", 
      " Example Text 4","Example 5", 


    }; 


// Keep all Images in array 
public Integer[] mThumbIds = { 
     R.drawable.img1, R.drawable.img2, 
     R.drawable.img3, R.drawable.img4, 
     R.drawable.img5, R.drawable.img6, 
     R.drawable.img7, R.drawable.img20, 
     R.drawable.img8, R.drawable.img21, 
     R.drawable.img9, R.drawable.img22, 
     R.drawable.img10, R.drawable.img23, 
     R.drawable.img11, R.drawable.img24, 
     R.drawable.img12, R.drawable.img25, 
     R.drawable.img13, R.drawable.img26, 
     R.drawable.img14, R.drawable.img27, 
     R.drawable.img15, R.drawable.img28, 
     R.drawable.img16, R.drawable.img29, 
     R.drawable.img17, R.drawable.img30, 
     R.drawable.img18, R.drawable.img31, 
     R.drawable.img19 
}; 

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

@Override 
public int getCount() { 
    return mThumbIds.length; 
} 

@Override 
public Object getItem(int position) { 
    return mThumbIds[position]; 
} 

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

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    ImageView imageView = new ImageView(mContext); 
    text=new TextView(mContext); 
    imageView.setImageResource(mThumbIds[position]); 
    imageView.setScaleType(ImageView.ScaleType.FIT_XY); 
    imageView.setLayoutParams(new GridView.LayoutParams(270, 270)); 
    text.setText(mThumbTxt[position]); 
    return imageView; 
} 

任何错误或什么有什么不对这个代码?我做错了什么? 单独的XML布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 
<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/ic_launcher" /> 
<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="TextView" /> 

+0

HTTP:/ /stackoverflow.com/questions/982386/android-simple-gridview-t帽子显示文本在网格 – Aamirkhan

回答

1

TextView没有显示因为你只有返回ImageView在你getView()方法的视图。

您需要创建单独的布局,因此您必须返回包含ImageViewTextView的布局。

OR

您可以创建一个LinearLayout动态地在其中添加ImageViewTextView和回报您LinearLayout

return layout; 

试试如下:

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    LinearLayout linearlayout=new LinearLayout(mContext); 
    ImageView imageView = new ImageView(mContext); 
    text=new TextView(mContext); 
    linearlayout.setOrientation(LinearLayout.VERTICAL); 
    imageView.setImageResource(mThumbIds[position]); 
    imageView.setScaleType(ImageView.ScaleType.FIT_XY); 
    imageView.setLayoutParams(new GridView.LayoutParams(270, 270)); 
    text.setText(mThumbTxt[position]); 

     linearlayout.addView(imageView); 
     linearlayout.addView(text); 
    return linearlayout; 
} 
+0

是的,这是工作,文本视图现在显示,但它是在图像的右侧,我如何使它在图像下? – user3094736

+1

@ user3094736使用'RelativeLayout'。或将方向设置为垂直 – Raghunandan

+0

制作'linearlayout.setOrientation(LinearLayout.VERTICAL);'。 @ user3094736 – GrIsHu

1

getview你有

return imageView; 

从而显示其正常的只有ImageView的。将ImageViewTextView包含在RelativeLayout/LinearLayout中并返回该视图。

或者用imageView和textView膨胀布局并返回视图。

编辑:

在构造

​​

然后在getView

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder holder; 
    if (convertView == null) { 
     convertView = mInflater.inflate(R.layout.yourlayout, 
         parent, false); 
     holder = new ViewHolder(); 
     holder.tv = (TextView) convertView.findViewById(R.id.textView1); 
     holder.iv = (ImageView) convertView.findViewById(R.id.imageView1); 
     convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 
      holder.iv.setImageResource(mThumbIds[position]); 
      holder.tv.setText(mThumbTxt[position]); 
    return convertView; 
} 

然后

static class ViewHolder 
{ 
    TextView tv; 
    ImageView iv; 
} 

参考:

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

How ListView's recycling mechanism works

+1

@ user3094736然后没有必要创建一个布局programaticalle – Raghunandan

+0

用户@GrlsHu使它工作,但它显示在图像的右侧,而不是下面。 – user3094736

+1

@ user3094736尝试编辑它更高效。 http://developer.android.com/training/improving-layouts/smooth-scrolling.html – Raghunandan

0

乘坐

ViewHolder holder; 
    holder = new ViewHolder(); 

与该图像和文字在LayoutInflater这个ViewHolder对象WD的帮助,并返回convertView为您getview 的paramerter这里ü只返回图像查看花花公子

环顾了一番后,我发现了一个很好的例子,在这里做到这一点: http://www.javacodegeeks.com/2013/08/android-custom-grid-view-example-with-image-and-text.html

+0

链接只有答案是不鼓励的。在你的答案或链接中添加链接的内容作为评论 – Raghunandan