我在关于GridView的android教程中遵循了这个例子,但是我想要使用TextView简单地显示一些文本,而不是显示图像.结果似乎比我想象的更难.看起来这似乎是完全没必要的,它没有一个有效的用例,但我正在尝试这个让自己熟悉sdk.
所以我的代码与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;
}
}
Run Code Online (Sandbox Code Playgroud)
这对我来说似乎都是有效的,但是运行它在屏幕上没有任何内容.并且没有错误消息.如果单击它们,会有一些可选择/可点击(不可见)的块,但文本显然没有显示.我想知道我的布局没有android:文本导致这个问题?或其他什么?
任何反馈将不胜感激,谢谢你的帮助!
我不确定是什么原因导致你的问题.我按照您链接到的页面上的分步说明来设置"Hello,GridView",并使用您的代码并能够看到文本.
我改变的唯一事情不是为ImageAdapter创建一个使用MyAdapter的类.在活动HelloGridView.java onCreate中,我使用了"MyAdapter"而不是"ImageAdapter".我根本没有改变布局.
alt text http://i41.tinypic.com/2rcwkmw.jpg
这是运行代码时获得的屏幕截图.
| 归档时间: |
|
| 查看次数: |
59522 次 |
| 最近记录: |