私のアダプタは、どのアクティビティがデータを送信しているかに応じて、1つのrow_layoutまたは別のrow_layoutを選択します。データを送信する2つのアクティビティがあります。異なるアクティビティと異なるrow_layoutsを持つ同じアダプタを使用したい
私はアダプタを1つのrow_layoutを使用する方法しか知りません。どのアクティビティがデータを送信しているかに応じて、別のコードを記述して別のrow_layoutを選択する方法を考えることができません。 (2番目のrow_layoutにはチェックボックスがありません)。ここで
私のアダプタです:あなたは活動が呼び出されると区別するためにShopItemAdapterコンストラクタにいくつかのパラメータを追加することができます
public class ShopItemAdapter extends ArrayAdapter<ShopItem> {
public ShopItemAdapter(Context context, ArrayList<ShopItem> shopItem){
super(context, 0, shopItem);
}
@NonNull
@Override
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
if(convertView == null){
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_layout, parent, false);
}
TextView tvItem = (TextView)convertView.findViewById(R.id.tvItemName);
TextView tvQty = (TextView)convertView.findViewById(R.id.tvQuantity);
CheckBox cbIsPurchased = (CheckBox)convertView.findViewById(R.id.cbIsPurchased);
ShopItem _shopItem = getItem(position);
tvItem.setText(_shopItem.getItemName());
tvQty.setText(String.valueOf(_shopItem.getQuantity()));
if(_shopItem.getIsPurchased() == 1){
cbIsPurchased.setChecked(true);
}
else{
cbIsPurchased.setChecked(false);
}
return convertView;
}
}
このアダプターを使用するアクティビティー数は固定されていますか? –
はい、私はこのアダプターを使用する2つのアクティビティーを持っています。 – Monkey