我有这个自定义适配器,我添加了这样的新项目;
card = new CardModel(name, email, image1);
card.setOnClickListener(new CardModel.OnClickListener() {
@Override
public void OnClickListener() {
Log.i("Swipeable Cards", "I am pressing the card");
Toast.makeText(getApplicationContext(), email.toString(), Toast.LENGTH_SHORT).show();
}
});
card.setOnCardDimissedListener(new CardModel.OnCardDimissedListener() {
@Override
public void onLike() {
Log.i("Swipeable Cards", "I dislike the card");
}
@Override
public void onDislike() {
Log.i("Swipeable Cards", "I like the card");
new sendNewYay().execute(sharedToken, name);
}
});
adapter.add(card);
mCardContainer.setAdapter(adapter);
在onDislike
方法中,我需要获取项目名称。
在这一行:
new sendNewYay().execute(sharedToken, name);
我发送项目名称,但它不起作用。
onLike
方法,另一个用于onDislike
方法。如何用我的按钮触发这两种方法?答案 0 :(得分:1)
我会改变你在CardModel.OnCardDimissedListener
界面中定义方法的方式,如下所示:
public interface OnCardDimissedListener {
void onLike(CardModel card);
void onDislike(CardModel card);
}
无论调用什么,这些方法都应该传递执行操作的CardModel
实例。那么你的听众可以得到这样的名字:
new CardModel.OnCardDimissedListener() {
@Override
public void onLike(CardModel card) {
Log.i("Swipeable Cards", "I dislike the card");
}
@Override
public void onDislike(CardModel card) {
Log.i("Swipeable Cards", "I like the card");
new sendNewYay().execute(sharedToken, card.getName());
}
});
答案 1 :(得分:0)
2.您可以在自定义适配器中实现click事件。就像这样:(我只使用BaseAdapter作为示例)
类Adapter扩展BaseAdapter {
private ArrayList<CardModel> list;
public int getCount() {
// TODO Auto-generated method stub
return list.length;
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row;
row = inflater.inflate(R.layout.custom, parent, false);
Button btn1 = (Button) row.findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// onLike method
}
});
Button btn2 = (Button) row.findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// onDislike Method and catch the name value
String name = list.get(position).getName;
}
});
return (row);
}
}
如果您想捕获主Activity中的onClick事件,可以将CardModelListener转移到自定义适配器中。因此,一旦用户单击该按钮,事件将被捕获在主Activity.Like:
中公共类MyActivity扩展了Activity { class CardModelListener实现CardModeInterface {
@Override
public void onLike(CardModel card) {
//onLike method
}
@Override
public void onDisLike(CardModel card) {
//onLike method
}
}
interface CardModeInterface {
onLike(CardModel card);
onDisLike(CardModel card);
}
}
并将CardModelListener的实例传输到自定义适配器:
类Adapter扩展BaseAdapter {
private CardModelListener listener;
public Adapter(CardModelListener onClickListener) {
this.listener = onClickListener;
}
public int getCount() {
// TODO Auto-generated method stub
return list.length;
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row;
row = inflater.inflate(R.layout.custom, parent, false);
Button btn1 = (Button) row.findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onLike(list.get(position);
}
});
return (row);
}
}
我认为这可以解决您的第一个问题。如果您需要更多详细信息,请与我们联系。