将编程布局添加到我的适配器时出现问题

发布于 2024-11-16 01:40:07 字数 2770 浏览 3 评论 0原文

这是我的适配器

     private class CustomAdapter extends BaseAdapter{

        public int getCount() {
            // TODO Auto-generated method stub
            return 5;
        }

        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            TextView text=new TextView(MainActivity.this);
            text.setText("SAmple text");
            Button button =(Button)new Button(MainActivity.this);
            button.setText("Click me");
            EditText edit=new EditText(MainActivity.this);
            LinearLayout layout=new LinearLayout(getApplicationContext());
            ImageView image=new ImageView(MainActivity.this);
            CheckBox check=new CheckBox(MainActivity.this);
            ToggleButton toggle=new ToggleButton(getApplicationContext());

            check.setChecked(true);
            layout.addView(check);
        layout.addView(toggle);




            if(position==0){
                convertView=text;

                }
            else if( position==1){
                convertView= button;
            }       
            else if(position==2){
                convertView= edit;
            }
            else if (position==3) {
                RelativeLayout rel=new View1(MainActivity.this);
                convertView=rel;
                }
            else{
                convertView=layout;
            }
            return convertView;
        }

这是我的自定义视图

package org.example.demo;



import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ToggleButton;

public class View1 extends RelativeLayout{


    public View1(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        setView(context);
    }

    public void setView(Context context){
        CheckBox check=new CheckBox(context);
        ToggleButton toggle=new ToggleButton(context);
        addView(check);
        addView(toggle);

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        // TODO Auto-generated method stub

    }

}

这是我得到的 在此处输入图像描述

问题是我无法获取在适配器外部创建的视图。在适配器内部创建视图或视图组或将其引用到外部不是同一件事吗?或者我错过了一些重写布局方法?

Here is my adapter

     private class CustomAdapter extends BaseAdapter{

        public int getCount() {
            // TODO Auto-generated method stub
            return 5;
        }

        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            TextView text=new TextView(MainActivity.this);
            text.setText("SAmple text");
            Button button =(Button)new Button(MainActivity.this);
            button.setText("Click me");
            EditText edit=new EditText(MainActivity.this);
            LinearLayout layout=new LinearLayout(getApplicationContext());
            ImageView image=new ImageView(MainActivity.this);
            CheckBox check=new CheckBox(MainActivity.this);
            ToggleButton toggle=new ToggleButton(getApplicationContext());

            check.setChecked(true);
            layout.addView(check);
        layout.addView(toggle);




            if(position==0){
                convertView=text;

                }
            else if( position==1){
                convertView= button;
            }       
            else if(position==2){
                convertView= edit;
            }
            else if (position==3) {
                RelativeLayout rel=new View1(MainActivity.this);
                convertView=rel;
                }
            else{
                convertView=layout;
            }
            return convertView;
        }

And here is my custom view

package org.example.demo;



import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ToggleButton;

public class View1 extends RelativeLayout{


    public View1(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        setView(context);
    }

    public void setView(Context context){
        CheckBox check=new CheckBox(context);
        ToggleButton toggle=new ToggleButton(context);
        addView(check);
        addView(toggle);

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        // TODO Auto-generated method stub

    }

}

Here is what I get
enter image description here

The problem is I can't get the view that I created outside of the adapter. Isn't it the same thing to create a view or viewgroup inside adapter or reference it to the outside? Or do I miss something overridding Layout methods?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文