如何在适配器中设置RelativeLayout的可见性和setText

时间:2019-07-19 12:38:15

标签: android adapter


我通过sqlite进行项目开发,并从数据库中获取银行卡清单!
我需要单击某些卡片后,在我的MainActivity中显示Relative并为此布局设置Text!

我的适配器:

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.CardViewHolder> {



private List<CardModel> cardModels;
private Context context;
public CardAdapter(Context context,List<CardModel> cardModels){

    this.cardModels=cardModels;
    this.context=context;

}

@NonNull
@Override
public CardViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View view= LayoutInflater.from(context).inflate(R.layout.cardbank_row,parent,false);
    return new CardViewHolder(view);

}

@Override
public void onBindViewHolder(@NonNull CardViewHolder holder, final int position) {

    CardModel cardModeles=cardModels.get(position);
    holder.bank.setText(cardModeles.getBank());
    holder.cardname.setText(cardModeles.getCardname());
    holder.cardnumber.setText(cardModeles.getCardnumber());
    holder.card.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

}

@Override
public int getItemCount() {

    return cardModels.size();

}

public class CardViewHolder extends RecyclerView.ViewHolder {

    TextView bank,cardnumber,cardname;
    ImageView bicon;
    CardView card;

    public CardViewHolder(@NonNull View itemView) {

        super(itemView);
        card=itemView.findViewById(R.id.cardbank_row);
        bicon=itemView.findViewById(R.id.iv_row_banklogo);
        bank=itemView.findViewById(R.id.iv_row_bankname);
        cardnumber=itemView.findViewById(R.id.tv_row_cardnumber);
        cardname=itemView.findViewById(R.id.tv_row_cardname);

    }
}

我的mainActivity:

public class MainActivity extends AppCompatActivity {

RelativeLayout add,close,card;
ImageView copy,share,notification;
TextView cardbank,cardnumber,cardname;
CoordinatorLayout coordinatorLayout;
private static final int TIME_INTERVAL = 2000;
private long mBackPressed;
MyDatabase myDatabase;
RecyclerView recyclerView;
private List<CardModel> cardModelList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    cast();
    dbcast();
    onClick();

}

public void cast(){

    add=findViewById(R.id.rv_btns_add);
    close=findViewById(R.id.rv_bank_close);
    copy=findViewById(R.id.iv_btn_copy);
    share=findViewById(R.id.iv_btn_share);
    notification=findViewById(R.id.iv_toolbar_notofication);
    coordinatorLayout=findViewById(R.id.mysnakbar);
    card=findViewById(R.id.rv_card_card);
    cardbank=findViewById(R.id.tv_bank_bankname);
    cardnumber=findViewById(R.id.tv_bnumber_cardnumber);
    cardname=findViewById(R.id.tv_name_cardname);
    cardModelList=new ArrayList<>();
    recyclerView=findViewById(R.id.rv_footer_list);
    recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this,RecyclerView.VERTICAL,false));



}

public void dbcast(){

    myDatabase=new MyDatabase(getApplicationContext());
    Cursor cursor=myDatabase.getInfos();
    for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){

        CardModel cardModel=new CardModel();
        cardModel.setId(cursor.getInt(0));
        cardModel.setBank(cursor.getString(1));
        cardModel.setCardnumber(cursor.getString(2));
        cardModel.setCardname(cursor.getString(3));

        cardModelList.add(cardModel);

    }

    recyclerView.setAdapter(new CardAdapter(MainActivity.this,cardModelList));

}

查看图片了解 demo what do i want

1 个答案:

答案 0 :(得分:0)

您可以通过将接口传递给适配器来实现。

public interface RecyclerCallback {

  void onClick(int pos);

}



public class CardAdapter extends RecyclerView.Adapter<CardAdapter.CardViewHolder> {

private List<CardModel> cardModels;
private Context context;
private RecyclerCallback callback;
public CardAdapter(Context context,List<CardModel> cardModels, RecyclerCallback callback){

    this.cardModels=cardModels;
    this.context=context;
    this.callback = callback;

}

@NonNull
@Override
public CardViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View view= LayoutInflater.from(context).inflate(R.layout.cardbank_row,parent,false);
    return new CardViewHolder(view);

}

@Override
public void onBindViewHolder(@NonNull CardViewHolder holder, final int position) {

    CardModel cardModeles=cardModels.get(position);
    holder.bank.setText(cardModeles.getBank());
    holder.cardname.setText(cardModeles.getCardname());
    holder.cardnumber.setText(cardModeles.getCardnumber());
    holder.card.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                callback.onClick(position); // or you can pass CardModel
        }
    });

}

@Override
public int getItemCount() {

    return cardModels.size();

}


public class MainActivity extends AppCompatActivity implements RecyclerCallback {

RelativeLayout add,close,card;
ImageView copy,share,notification;
TextView cardbank,cardnumber,cardname;
CoordinatorLayout coordinatorLayout;
private static final int TIME_INTERVAL = 2000;
private long mBackPressed;
MyDatabase myDatabase;
RecyclerView recyclerView;
private List<CardModel> cardModelList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    cast();
    dbcast();
    onClick();

}

public void cast(){

    add=findViewById(R.id.rv_btns_add);
    close=findViewById(R.id.rv_bank_close);
    copy=findViewById(R.id.iv_btn_copy);
    share=findViewById(R.id.iv_btn_share);
    notification=findViewById(R.id.iv_toolbar_notofication);
    coordinatorLayout=findViewById(R.id.mysnakbar);
    card=findViewById(R.id.rv_card_card);
    cardbank=findViewById(R.id.tv_bank_bankname);
    cardnumber=findViewById(R.id.tv_bnumber_cardnumber);
    cardname=findViewById(R.id.tv_name_cardname);
    cardModelList=new ArrayList<>();
    recyclerView=findViewById(R.id.rv_footer_list);
    recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this,RecyclerView.VERTICAL,false));



}

public void dbcast(){

    myDatabase=new MyDatabase(getApplicationContext());
    Cursor cursor=myDatabase.getInfos();
    for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){

        CardModel cardModel=new CardModel();
        cardModel.setId(cursor.getInt(0));
        cardModel.setBank(cursor.getString(1));
        cardModel.setCardnumber(cursor.getString(2));
        cardModel.setCardname(cursor.getString(3));

        cardModelList.add(cardModel);

    }

    recyclerView.setAdapter(new CardAdapter(MainActivity.this,cardModelList, this));

}

@Override
onClick(int pos){
    CardModel model = cardModelList.get(pos);
    // write code to show data in relative layout 

}

}