我想在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();
答案 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){
}