问题描述
我下面就关于在GridView Android的教程中的例子,但不是展示形象,我想只是简单的显示使用一个TextView一些文字。事实证明似乎比我想象的更难。它可能看起来这是完全不必要的,它并没有一个有效的用例,但我想这一点,只是让自己熟悉SDK。
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.
让我的code是pretty的大致相同的的>,而是使用ImageAdapter,我创建了一个虚拟的适配器类似以下内容:
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:
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;
}
}
这一切似乎有效的给我,但运行该给我什么在屏幕上。而且也没有错误消息。也有一些可选择/点击(无形)块,如果我点击它们,但是文字是显而易见的未显示。我不知道是我的布局没有了android:文本造成这个问题?还是别的什么?
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?
任何反馈将是AP preciated并感谢您的帮助!
any feedback will be appreciated and thanks for your help!
推荐答案
我不知道这可能是造成你的问题。我跟着你链接到设立你好,GridView控件页面上的一步一步的指示,并用你的code,并能看到的文字。
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.
我改变的唯一的事情是,而不是为ImageAdapter创建一个类我用你的MyAdapter。在活动HelloGridView.java的onCreate我用MyAdapter,而不是ImageAdapter。我没有改变的布局都没有。
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.
下面是运行您的code时,我所得到的截图。
Here is a Screenshot of what I get when running your code.
这篇关于Android的:简单的GridView,在网格显示文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!