android 可扩展列表需要帮助

发布于 2024-11-11 17:00:14 字数 1526 浏览 9 评论 0原文

public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    View v = null;
    if( convertView != null )
        v = convertView;
    else
        v = inflater.inflate(R.layout.child_row, parent, false); 
    Color c = (Color)getChild( groupPosition, childPosition );
    TextView color = (TextView)v.findViewById( R.id.childname );
    if( color != null )
        color.setText( c.getColor() );
    TextView rgb = (TextView)v.findViewById( R.id.rgb );
    if( rgb != null )
        rgb.setText( c.getRgb() );
    //  CheckBox cb = (CheckBox)v.findViewById( R.id.check1 );
    //  cb.setChecked( c.getState() );
    Button btnPlay=(Button)v.findViewById(R.id.Play);
    Button btnDelete=(Button)v.findViewById(R.id.Delete);
    Button btnEmail=(Button)v.findViewById(R.id.Email);
    btnPlay.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Log.i("TEST ", "play btn cliked");

            Intent i=new Intent(this,FindFilesByType.class);//THIS IS NOT ALLOWED IN MY CODE

        }
    });

    return v;
}

在上面的代码中,我编写了我的适配器类。 我小时候的可解释列表中有 3 个按钮。 我想在按钮的单击事件上调用另一个Java类, 所以,当我使用:

Intent i=new Intent(this,FindFilesByType.class);

我得到这个:

The constructor Intent(new View.OnClickListener(){}, Class<FindFilesByType>) is undefined
Remove arguments to match 'Intent'

我正在做的事情出了什么问题?

public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    View v = null;
    if( convertView != null )
        v = convertView;
    else
        v = inflater.inflate(R.layout.child_row, parent, false); 
    Color c = (Color)getChild( groupPosition, childPosition );
    TextView color = (TextView)v.findViewById( R.id.childname );
    if( color != null )
        color.setText( c.getColor() );
    TextView rgb = (TextView)v.findViewById( R.id.rgb );
    if( rgb != null )
        rgb.setText( c.getRgb() );
    //  CheckBox cb = (CheckBox)v.findViewById( R.id.check1 );
    //  cb.setChecked( c.getState() );
    Button btnPlay=(Button)v.findViewById(R.id.Play);
    Button btnDelete=(Button)v.findViewById(R.id.Delete);
    Button btnEmail=(Button)v.findViewById(R.id.Email);
    btnPlay.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Log.i("TEST ", "play btn cliked");

            Intent i=new Intent(this,FindFilesByType.class);//THIS IS NOT ALLOWED IN MY CODE

        }
    });

    return v;
}

In the code above, I've written my adapterclass.
There are 3 buttons in my explandable list as a child.
I want to call another Java class on button's click event,
so, when I am using:

Intent i=new Intent(this,FindFilesByType.class);

I am getting this:

The constructor Intent(new View.OnClickListener(){}, Class<FindFilesByType>) is undefined
Remove arguments to match 'Intent'

What is wrong with what I am doing?

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

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

发布评论

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

评论(3

秋日私语 2024-11-18 17:00:14

Intent 构造函数中的 this 对象是匿名 OnClickListener 类实例,而不是当前上下文。您需要做的是在该构造函数中使用当前上下文,例如:

Intent i = new Itent(getContext(), FindFilesByType.class);

The this object in your Intent constructor is the anonymous OnClickListener class instance, as opposed to your current context. What you need to do is to use the current context in that constructor like:

Intent i = new Itent(getContext(), FindFilesByType.class);
双马尾 2024-11-18 17:00:14

首先,在 onCreate() 方法之前创建实例变量,例如

public static InstanceTest instance;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    instance=this;
}

并在单击事件时在您的孩子中调用该变量

public void onClick(View arg0) {
    Intent i=new Intent(instance,FindFilesByType.class);//This time it will be allowed
}

希望这可以有所帮助。

At the start, create intance variable before onCreate() method like

public static InstanceTest instance;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    instance=this;
}

And called that variable in your child on click event

public void onClick(View arg0) {
    Intent i=new Intent(instance,FindFilesByType.class);//This time it will be allowed
}

Hope this can help.

满地尘埃落定 2024-11-18 17:00:14
public View getChildView(int groupPosition, int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {
    View v = null;
    if (convertView != null)
        v = convertView;
    else
        v = inflater.inflate(R.layout.child_row, parent, false);
    Rington c = (Rington) getChild(groupPosition, childPosition);
    TextView color = (TextView) v.findViewById(R.id.childname);
    if (color != null)
        color.setText(c.getColor());
    TextView rgb = (TextView) v.findViewById(R.id.rgb);
    if (rgb != null)
        rgb.setText(c.getRgb());

    Button btnPlay = (Button) v.findViewById(R.id.Play);
    Button btnDelete = (Button) v.findViewById(R.id.Delete);
    Button btnEmail = (Button) v.findViewById(R.id.Email);
    btnPlay.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            //code

        }
    });
    btnDelete.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            //code

        }
    });
    btnEmail.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            //code
        }
    });

    return v;
}

将上述方法添加到适配器类中,这对我来说是完全有效的代码:)

public View getChildView(int groupPosition, int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {
    View v = null;
    if (convertView != null)
        v = convertView;
    else
        v = inflater.inflate(R.layout.child_row, parent, false);
    Rington c = (Rington) getChild(groupPosition, childPosition);
    TextView color = (TextView) v.findViewById(R.id.childname);
    if (color != null)
        color.setText(c.getColor());
    TextView rgb = (TextView) v.findViewById(R.id.rgb);
    if (rgb != null)
        rgb.setText(c.getRgb());

    Button btnPlay = (Button) v.findViewById(R.id.Play);
    Button btnDelete = (Button) v.findViewById(R.id.Delete);
    Button btnEmail = (Button) v.findViewById(R.id.Email);
    btnPlay.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            //code

        }
    });
    btnDelete.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            //code

        }
    });
    btnEmail.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            //code
        }
    });

    return v;
}

this above methos into the adapter class this is fully working code for me :)

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