创建构造函数字符串string1,字符串string2,字符串string3,Object object1

时间:2013-07-05 09:10:12

标签: java android object constructor

我想在MainActivity中创建一个类:

MyCard customCard1 = new MyCard(cardTitle.getText().toString(),
                cardbuttonon.getText().toString(),
        cardbuttonon.getText().toString(),
        cardbuttonon.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String[] url={"url"};
                new onoff(MainActivity.this).execute(url);
            }});
        mCardView.addCard(customCard1);
        mCardView.refresh();

但他告诉我构造函数MyCard(String,String,String,Object undefined is。

我如何在我的MyCard上定义他:

package de.heron.cloudbox;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.fima.cardsui.objects.Card;

public class MyCard extends Card {

    public MyCard(String title, String desc, String string, Object setOnClickListener){
        super(title, desc); 
    }
    @Override
    public View getCardContent(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.card_ex, null);

        ((TextView) view.findViewById(R.id.title)).setText(title);
        ((TextView) view.findViewById(R.id.description)).setText(desc);

        return view;
    }
}

有人可以帮我解决问题吗?

为什么我可以使用2个onclicklisteners:

MyCardXml customCard1 = new MyCardXml(cardTitle.getText().toString(),cardbuttonon = (Button) findViewById(R.id.cardbuttonon),cardbuttonoff = (Button) findViewById(R.id.cardbuttonoff),
                cardbuttonon.getText().toString(),cardbuttonoff.getText().toString(),
        cardbuttonon.getText().toString(),cardbuttonoff.getText().toString(),
        cardbuttonon.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                String[] url={"http://everhome.de/api/applive/", "8ef43502ad3dc04f87b4a48b993878c0","/" + KEY_ID,"/" + KEY_COST};
                new onoff(MainActivity.this).execute(url);
                }}

                ),cardbuttonoff.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    String[] url={"http://everhome.de/api/applive/", "8ef43502ad3dc04f87b4a48b993878c0","/" + KEY_ID,"/" + KEY_COST};
                    new onoff(MainActivity.this).execute(url);
                }}

                ));
        mCardView.addCard(customCard1);
        mCardView.refresh();

3 个答案:

答案 0 :(得分:1)

您应该将View.OnClickListener传递给MyCard构造函数而不是Object

更改

   public MyCard(String title, String desc, String string, Object setOnClickListener){
        super(title, desc); 
    }

   public MyCard(String title, String desc, String string,  View.OnClickListener setOnClickListener){

答案 1 :(得分:1)

您的构造函数需要 Object 作为第四个参数。您没有在第四个参数中传递对象 你正在构建对象。这将解决错误

MyCard customCard1 = new MyCard(cardTitle.getText().toString(),
            cardbuttonon.getText().toString(),
    cardbuttonon.getText().toString(),
    new View.OnClickListener() {
        public void onClick(View v) {
            String[] url={"url"};
            new onoff(MainActivity.this).execute(url);
        }});

答案 2 :(得分:0)

您必须像这样更改构造函数

public MyCard(String title, String desc, String string, OnClickListener clickListener){

}