使用自定义适配器中的动态元素
你好,这是我的第一个问题。 我正在创建本教程的动态网格 http://www.stealthcopter.com/blog/2010/09/android-creating-a-custom-adapter-for-gridview-buttonadapter/comment-page-1/
现在是工作得很好。我的布局由 gridView 组成,在该 gridView 下我有一个 TextView。
问题是我想更改 TextView 以在焦点更改(在网格元素上)时在每个 id 上显示不同的信息。我尝试在 ButtonAdapter 内使用 OnFocusChangeListener,但是当尝试获取对 textView 的引用时,它说 findViewById 未实现。
我想知道如何在我的主要活动中进行引用,以允许我处理动态网格元素。 我在 onCreate() 中有以下内容;
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ButtonAdapter(this));
所以我想从这里处理我的网格元素,有什么想法吗? 谢谢
编辑: 我一直在尝试改变不同的事情,但我从 getView 方法收到 NullPointerException 。我找不到办法让它工作,我会感谢任何帮助的人,这是我的代码:
public View getView(int position, View convertView, ViewGroup parent) {
final Button btn;
if (convertView == null) {
// if it's not recycled, initialize some attributes
btn = new Button(mContext);
btn.setLayoutParams(new GridView.LayoutParams(100, 55));
btn.setPadding(8, 8, 8, 8);
} else {
btn = (Button) convertView;
}
btn.setText(filenames[position]);
// filenames is an array of strings
btn.setTextColor(Color.WHITE);
btn.setId(position);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
TextView vt = (TextView) btn.findViewById(R.id.textView1);
vt.setText("Button Pressed");
}
});
谢谢。
Hellow this is my first question in stack.
Im creating a dynamic grid of this tutorial
http://www.stealthcopter.com/blog/2010/09/android-creating-a-custom-adapter-for-gridview-buttonadapter/comment-page-1/
Now it's working pretty good. My layout is composed by a gridView and under this gridView i have a TextView.
The problem is that i want to change the TextView to display different information on each id when the focus changes (on the grid elements). I've tried to use OnFocusChangeListener inside ButtonAdapter, but when trying to get a reference to the textView, it says that findViewById is not implemented.
I wonder how to make a reference in my main activity that allows me to handle my dynamic grid elements.
I have the following in onCreate();
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ButtonAdapter(this));
So i want to handle my grid elements from here, any ideas?
Thanks
Edit:
I've been trying to change different things, but im receiving a NullPointerException from my getView method. I can't find a way to make it work, i'll apreciate any help guys, this is my code:
public View getView(int position, View convertView, ViewGroup parent) {
final Button btn;
if (convertView == null) {
// if it's not recycled, initialize some attributes
btn = new Button(mContext);
btn.setLayoutParams(new GridView.LayoutParams(100, 55));
btn.setPadding(8, 8, 8, 8);
} else {
btn = (Button) convertView;
}
btn.setText(filenames[position]);
// filenames is an array of strings
btn.setTextColor(Color.WHITE);
btn.setId(position);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
TextView vt = (TextView) btn.findViewById(R.id.textView1);
vt.setText("Button Pressed");
}
});
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你走在正确的轨道上,但我认为在你的适配器中处理它是最有意义的。因此 findViewById() 不适合您,但如果您将其更改为 ConvertView.findViewById() (或适配器的 getView 方法中返回的任何视图),它将起作用。从那里您将能够操作 TextView。
I think you are on the right track, but I think it would make most sense to take care of it in your adapter. So findViewById() isn't working for you, but it will work if you change it to convertView.findViewById() (or whatever view is returned in your getView method of your Adapter). From there you will be able to manipulate the TextView.