使用自定义适配器中的动态元素

发布于 2024-11-30 09:14:44 字数 1643 浏览 1 评论 0原文

你好,这是我的第一个问题。 我正在创建本教程的动态网格 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南城旧梦 2024-12-07 09:14:44

我认为你走在正确的轨道上,但我认为在你的适配器中处理它是最有意义的。因此 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文