英文:
how to set listener for items in adapter?
问题
bankCardAdapter:
Have a lyser on the adapter to change something when the method clicks.
I dont know what i shoud write on body of onClickListener.
And listener.onClick(); needs (View View,StructCard data,int posotion)
Thank you in advance And I'm sorry for my weakness in English
英文:
Have a lyser on the adapter to change something when the method clicks.
I dont know what i shoud write on body of onClickListener.
And listener.onClick(); needs (View View,StructCard data,int posotion)
Thank you in advance And I'm sorry for my weakness in English
bankCardAdapter:
package ir.acopal.android.test.adapter;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.Random;
import ir.acopal.android.test.R;
import ir.acopal.android.test.classes.StructCard;
import ir.acopal.android.test.interfaces.setOnclickListenerBankCard;
import ir.acopal.android.test.view.CustomBankCard;
public class BankCardAdapter extends RecyclerView.Adapter<BankCardAdapter.ViewHolder> {
private ArrayList<StructCard> accountCards;
private Context context;
setOnclickListenerBankCard listener;
Boolean a = true;
View view;
public BankCardAdapter(ArrayList<StructCard> accountCards, Context context) {
this.accountCards = accountCards;
this.context = context;
}
public void setListener(setOnclickListenerBankCard listener) {
this.listener = listener;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.test, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
StructCard cur = accountCards.get(position);
if (a) {
int firstCard = Color.parseColor("#BDBAAB");
holder.card.setCardBackgroundColor(firstCard);
a = false;
} else {
Random rnd = new Random();
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
holder.card.setCardBackgroundColor(color);
}
holder.card.setCardNumber(cur.getAccountCardNumbers());
holder.card.setCustomCardExpireDateText(cur.getAccountExpirationDate());
holder.card.setNewCardText("");
// holder.txtExpirationDate.setText(number.getAccountExpirationDate());
}
@Override
public int getItemCount() {
return accountCards.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
CustomBankCard card;
public ViewHolder(@NonNull final View itemView) {
super(itemView);
findViews(itemView);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
private void findViews(View itemView) {
card = itemView.findViewById(R.id.itemBankCard_cbcCard);
}
}
}
Struct card:
package ir.acopal.android.test.classes;
public class StructCard {
private String accountCardNumbers;
private String accountExpirationDate;
private boolean empty;
public String getAccountCardNumbers() {
return accountCardNumbers;
}
public void setAccountCardNumbers(String accountCardNumbers) {
this.accountCardNumbers = accountCardNumbers;
}
public String getAccountExpirationDate() {
return accountExpirationDate;
}
public void setAccountExpirationDate(String accountExpirationDate) {
this.accountExpirationDate = accountExpirationDate;
}
public boolean isEmpty() {
return empty;
}
public void setEmpty(boolean empty) {
this.empty = empty;
}
}
listener:
package ir.acopal.android.test.interfaces;
import android.view.View;
import ir.acopal.android.test.classes.StructCard;
public interface setOnclickListenerBankCard {
void onClick(View view, StructCard data, int posotion);
}
答案1
得分: 0
In your onCreateViewHolder 方法中添加以下代码。你可以通过适配器的位置获取 structCard 的值。
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.test, parent, false);
ViewHolder holder = new ViewHolder(view);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "position = " + holder.getAdapterPosition());
StructCard structCard = accountCards[holder.getAdapterPosition()];
}
});
return holder;
}
英文:
on your onCreateViewHolder add the following code. you will get structCard value by using adapter position
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.test, parent, false);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "position = " + holder.getAdapterPosition());
StructCard structCard = accountCards[holder.getAdapterPosition()]
}
});
return new ViewHolder(view);
}
答案2
得分: 0
请将您的onBindViewHolder
更改如下:
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
StructCard cur = accountCards.get(position);
if (a) {
int firstCard = Color.parseColor("#BDBAAB");
holder.card.setCardBackgroundColor(firstCard);
a = false;
} else {
Random rnd = new Random();
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
holder.card.setCardBackgroundColor(color);
}
holder.card.setCardNumber(cur.getAccountCardNumbers());
holder.card.setCustomCardExpireDateText(cur.getAccountExpirationDate());
holder.card.setNewCardText("");
/*在此处添加以下行以进行项目单击。单击项目后,调用listener.onClick并传递所需的参数。 */
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onClick(holder.itemView, cur, position)
}
});
}
英文:
Change your onBindViewHolder
like this.
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
StructCard cur = accountCards.get(position);
if (a) {
int firstCard = Color.parseColor("#BDBAAB");
holder.card.setCardBackgroundColor(firstCard);
a = false;
} else {
Random rnd = new Random();
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
holder.card.setCardBackgroundColor(color);
}
holder.card.setCardNumber(cur.getAccountCardNumbers());
holder.card.setCustomCardExpireDateText(cur.getAccountExpirationDate());
holder.card.setNewCardText("");
/*Add these lines for item click. After Click on item
call listener.onClick and pass requires parameters. */
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onClick(holder.itemView, cur, position)
}
});
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论