Java Android:在网格中显示文本的简单 GridView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/982386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: Simple GridView that displays text in the grids
提问by fei
I'm following the example on the android tutorial about the GridView, but instead of showing image, i want to just simple show some text using a TextView. it turns out seems to be harder than i thought. it might seems like this is totally unnecessary and it doesn't have a valid use case, but i'm trying this out to just get myself familiar with the sdk.
我正在关注关于 GridView 的 android 教程中的示例,但我不想显示图像,而是想使用 TextView 简单地显示一些文本。结果似乎比我想象的要难。这似乎是完全没有必要的,并且它没有有效的用例,但我正在尝试这样做只是为了让自己熟悉 sdk。
so my code is pretty much the same as the GridView example in http://developer.android.com/guide/tutorials/views/hello-gridview.html, but instead of using a ImageAdapter, i created a dummy adapter like following:
所以我的代码与http://developer.android.com/guide/tutorials/views/hello-gridview.html 中的 GridView 示例几乎相同,但我没有使用 ImageAdapter,而是创建了一个虚拟适配器,如下所示:
public class MyAdapter extends BaseAdapter {
private Context context;
private String[] texts = {"aaa", "bbb", "ccc", "ddd", "eee", "fff", "eee", "hhh", "iii"};
public MyAdapter(Context context) {
this.context = context;
}
public int getCount() {
return 9;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv;
if (convertView == null) {
tv = new TextView(context);
tv.setLayoutParams(new GridView.LayoutParams(85, 85));
}
else {
tv = (TextView) convertView;
}
tv.setText(texts[position]);
return tv;
}
}
it all seems valid to me, but running this gives me nothing on the screen. and there's no error message. there are some selectable/clickable (invisible) blocks if i click them, but the text is obvious not shown. i wonder is my layout doesn't have the android:text causing this problem? or anything else?
这一切对我来说似乎都是有效的,但是运行它不会在屏幕上显示任何内容。并且没有错误信息。如果我单击它们,则有一些可选择/可单击(不可见)块,但文本明显未显示。我想知道是我的布局没有导致这个问题的 android:text 吗?还是别的什么?
any feedback will be appreciated and thanks for your help!
任何反馈将不胜感激,并感谢您的帮助!
采纳答案by snctln
I am not sure what could be causing your problem. I followed the step by step instructions on the page that you linked to to set up "Hello, GridView", and used your code and was able to see the text.
我不确定是什么导致了您的问题。我按照您链接到的页面上的分步说明设置“Hello, GridView”,并使用您的代码并能够看到文本。
The only things I changed was rather than creating a class for ImageAdapter I used your MyAdapter. In the activity HelloGridView.java onCreate I used "MyAdapter" rather than "ImageAdapter". I didn't change the layout at all.
我唯一改变的是,我没有为 ImageAdapter 创建一个类,而是使用了你的 MyAdapter。在活动 HelloGridView.java onCreate 中,我使用了“MyAdapter”而不是“ImageAdapter”。我根本没有改变布局。
Here is a Screenshot of what I get when running your code.
这是我在运行您的代码时得到的屏幕截图。
回答by ist_lion
I see GridView so I'm almost assuming that this is similar to SWT?
我看到 GridView 所以我几乎假设这类似于 SWT?
If so you need to show the relationship between your view and the ViewGroup parent
如果是这样,您需要显示您的视图和 ViewGroup 父级之间的关系
回答by HairyLemon
I don't think your getItem implementation is correct. Top of my head it should be
我认为您的 getItem 实现不正确。我的头顶应该是
return texts[position];


