android listView 的使用方法和adapter的使用

public class RecyclerAdapterextends RecyclerView.Adapter {

                private Contextcontext;

                private Listmlist;

                 public RecyclerAdapter(Context context,List mlist){

                 this.context =context;

                 this.mlist =mlist;

}

@NonNull

@Override

    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup,int i) {

       View view = LayoutInflater.from(context).inflate(R.layout.activity_transtions_item,

          viewGroup,false);

           return new ViewHolder(view);

}

@Override

  public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder,int i) {

              if (viewHolderinstanceof ViewHolder){

                     ViewHolder holder = (ViewHolder) viewHolder;

                       final Transactions transactions = (Transactions)mlist.get(i);

Gson gson =new Gson();

Log.i("adapter渲染item",gson.toJson(transactions));

holder.linearItem.setOnClickListener(new View.OnClickListener() {

@Override

                public void onClick(View v) {

Intent intent =new Intent(context,TransactionsDetailActivity.class);

intent.putExtra("data",transactions);

context.startActivity(intent);

}

});

holder.rAddress.setText(EllipsizeAddress(transactions.getCounterparty()));

SimpleDateFormat simpleDateFormat =new SimpleDateFormat("MM-dd HH:mm:ss");

Date date =new Date(transactions.getDate().longValue()*1000);

holder.rDate.setText(simpleDateFormat.format(date));

if (("sent").equals(transactions.getType())){

holder.tvPayNum.setVisibility(View.GONE);

holder.tvPayName.setVisibility(View.GONE);

holder.rName.setText(transactions.getAmount().getCurrency());

holder.rImg.setImageResource(R.drawable.ic_transfer_send);

holder.rNum.setText("-"+transactions.getAmount().getValue());

holder.rNum.setTextColor(Color.RED);

}else if (("received").equals(transactions.getType())){

holder.tvPayNum.setVisibility(View.GONE);

holder.tvPayName.setVisibility(View.GONE);

holder.rName.setText(transactions.getAmount().getCurrency());

holder.rImg.setImageResource(R.drawable.ic_transfer_receive);

holder.rNum.setText("+"+transactions.getAmount().getValue());

holder.rNum.setTextColor(context.getResources().getColor(R.color.color_detail_receive));

}else if (("offernew").equals(transactions.getType())){

holder.tvPayNum.setVisibility(View.VISIBLE);

holder.tvPayName.setVisibility(View.VISIBLE);

holder.rImg.setImageResource(R.drawable.ic_offer_new);

holder.rAddress.setText("创建委托");

holder.tvPayNum.setText(transactions.getPays().getValue());

holder.tvPayName.setText(transactions.getPays().getCurrency());

holder.rNum.setText("→"+transactions.getGets().getValue());

holder.rName.setText(transactions.getGets().getCurrency());

}else if (("offercancel").equals(transactions.getType())){

holder.tvPayNum.setVisibility(View.VISIBLE);

holder.tvPayName.setVisibility(View.VISIBLE);

holder.rImg.setImageResource(R.drawable.ic_offer_cancel);

holder.rAddress.setText("取消委托");

holder.tvPayNum.setText(transactions.getPays().getValue());

holder.tvPayName.setText(transactions.getPays().getCurrency());

holder.rNum.setText("→"+transactions.getGets().getValue());

holder.rName.setText(transactions.getGets().getCurrency());

}else if (("offereffect").equals(transactions.getType())){

holder.tvPayNum.setVisibility(View.VISIBLE);

holder.tvPayName.setVisibility(View.VISIBLE);

holder.rImg.setImageResource(R.drawable.ic_offer_effect);

holder.rAddress.setText("委托成交");

}

}

}

@Override

    public int getItemCount() {

return mlist.size();

}

static class ViewHolderextends RecyclerView.ViewHolder{

ImageViewrImg;

TextViewrAddress,rDate,rNum,rName,tvPayNum,tvPayName;

LinearLayoutlinearItem;

public ViewHolder(@NonNull View itemView) {

super(itemView);

rImg =itemView.findViewById(R.id.rImg);

rAddress =itemView.findViewById(R.id.rAddress);

rDate =itemView.findViewById(R.id.rDate);

rNum=itemView.findViewById(R.id.tv_jy_num);

rName =itemView.findViewById(R.id.tv_name);

tvPayName=itemView.findViewById(R.id.tv_pay_name);

tvPayNum=itemView.findViewById(R.id.tv_pay_num);

linearItem =itemView.findViewById(R.id.linear_item);

}

}

private String EllipsizeAddress(String address){

if (Wallet.isValidAddress(address)){

String startStr =address.substring(0,3);

String endStr =address.substring(address.length()-4);

return startStr+"***"+endStr;

}

return address;

}

}

你可能感兴趣的:(android listView 的使用方法和adapter的使用)