在Recycler View中添加和显示数据

时间:2017-03-23 10:05:29

标签: java android android-recyclerview cardview

我遇到了问题所以我遇到了问题。

enter image description here 在扫描活动中,扫描QR码并生成卡数据,然后当值匹配时,我们转到下一个活动,我们将显示下一个值。单击Next按钮并转到我们的数据列表。询问如何从ScanActivity.class活动卡数据中获取并添加列表

来自ScanActitivty.java的

代码

if (!p.matcher(decodedBarcodeValue).matches()) {
                            showAlterDialog("Błędny QR Kod", "Podany kod QR code jest błędny. Zeskanuj go ponownie.");

                        } else {

                            Matcher m = p.matcher(decodedBarcodeValue);

                            while (m.find()) {
                                daneKarty[index] = m.group(1);
                                index++;
                            }

                            if (daneKarty.length < 6) {
                                showAlterDialog("Błędny  QR Kod", "Podany Kod jest nieprawidłowy");
                            }

                            sciezka3 = daneKarty[0];
                            base32 = daneKarty[1];
                            nameCard = daneKarty[2];
                            intervalTotp = daneKarty[3];
                            passwordHotp = daneKarty[4];
                            expirationDate = daneKarty[5];

                            if (intervalTotp.equals("")) {
                                intervalTotp = "60";

                            }

                            try {
                                OTP = generateOTP(base32, uuidDevice);
                            } catch (UnsupportedEncodingException e) {
                                e.printStackTrace();
                            }
                            Base32 code = new Base32();
                            byte secret[] = code.decode(OTP);

                            try {
                                hotpValue = Hotp.generateHotp(secret, hotp_counter, 6);
                            } catch (NoSuchAlgorithmException e) {
                                e.printStackTrace();
                            } catch (InvalidKeyException e) {
                                e.printStackTrace();
                            }

                            if (hotpValue.equals(passwordHotp)) {
                                Intent intent = new Intent(ScanQrCodeActivity.this, Stage3Activity.class);
                                intent.putExtra(EXTRA_MESSAGE, hotpValue);
                                startActivityForResult(intent, REQUEST_CODE);
                            } else {
                                showAlterDialog("Błędny  QR Kod", "Podany Kod jest nieprawidłowy");
                            }

Stage3Activity.class

public class Stage3Activity extends AppCompatActivity {

    private String hotpValueString;

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stage3);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.drawable.ic_restart);

        hotpValueString = getIntent().getStringExtra("hotpValume");

        Typeface custom_fonts = Typeface.createFromAsset(getAssets(), "fonts/OpenSans-Regular.ttf");
        Typeface custom_fonts2 = Typeface.createFromAsset(getAssets(), "fonts/OpenSans-Bold.ttf");

        TextView title_activity = (TextView) findViewById(R.id.stage3TileActivity);
        title_activity.setTypeface(custom_fonts);

        TextView stageOneText = (TextView) findViewById(R.id.stageOneText);
        stageOneText.setTypeface(custom_fonts);

        TextView stageTwoText = (TextView) findViewById(R.id.stageTwoText);
        stageTwoText.setTypeface(custom_fonts);

        TextView stageThreeText = (TextView) findViewById(R.id.stageThreeText);
        stageThreeText.setTypeface(custom_fonts);

        TextView messageView = (TextView) findViewById(R.id.QuestionTextView);
        messageView.setTypeface(custom_fonts);

        TextView valueHotp = (TextView) findViewById(R.id.valueHotpTextView);
        valueHotp.setTypeface(custom_fonts2);

        valueHotp.setText(hotpValueString);

        TextView yesButton = (TextView) findViewById(R.id.yesButton);
        yesButton.setTypeface(custom_fonts);

        yesButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Stage3Activity.this, MainActivity.class);
                startActivity(intent);
            }
        });
}

MainActivity.class

public class MainActivity extends AppCompatActivity {


    public List<Card> cardList = new ArrayList<>();

    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(newBase);
    }

    private RecyclerView cardRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        Typeface custom_fonts = Typeface.createFromAsset(getAssets(), "fonts/OpenSans-Regular.ttf");

        cardRecyclerView = (RecyclerView) findViewById(R.id.cardViewRecycleView);

        mLayoutManager = new LinearLayoutManager(this);
        cardRecyclerView.setLayoutManager(mLayoutManager);

        // title application
        TextView title_app = (TextView) findViewById(R.id.toolbar_title);
        title_app.setTypeface(custom_fonts);

    }

班级

public class Card {

    private String nameCard;
    private String dateCard;
    private String dateExpiration;

    public Card(String nameCard, String dateCard, String dateExpiration) {
        this.nameCard = nameCard;
        this.dateCard = dateCard;
        this.dateExpiration = dateExpiration;
    }

    public String getNameCard() {
        return nameCard;
    }

    public void setNameCard(String nameCard) {
        this.nameCard = nameCard;
    }

    public String getDateCard() {
        return dateCard;
    }

    public void setDateCard(String dateCard) {
        this.dateCard = dateCard;
    }

    public String getDateExpiration() {
        return dateExpiration;
    }

    public void setDateExpiration(String dateExpiration) {
        this.dateExpiration = dateExpiration;
    }
}

CardAdapter.class

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

    private List<Card> cardsList = new ArrayList<>();
    private Card card;

    public CardAdapter( List<Card> cardsList) {
        this.cardsList = cardsList;
    }

    //dodaj obiekt do listy
    private void addItem(int position, Card card) {
        cardsList.add(position, card);
        notifyItemChanged(position);
    }


    public class CardViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        public CardView cardview;
        public TextView nameCard, dateText, setDateText, dateExpirationText, setDateExpirationText;
        public ImageView mImageButton;
        Typeface custom_fonts = Typeface.createFromAsset(itemView.getContext().getAssets(), "fonts/OpenSans-Regular.ttf");


        public CardViewHolder(View itemView) {
            super(itemView);
            //  name Card
            nameCard = (TextView) itemView.findViewById(R.id.nameCard);
            nameCard.setTypeface(custom_fonts);

            //data add Card
            setDateText = (TextView) itemView.findViewById(R.id.setDateText);
            setDateText.setTypeface(custom_fonts);

            dateText = (TextView) itemView.findViewById(R.id.dateText);
            dateText.setTypeface(custom_fonts);

            mImageButton = (ImageView) itemView.findViewById(R.id.garbageDelete);

            //data expiration date
            dateExpirationText = (TextView) itemView.findViewById(R.id.dateExpiration);
            dateExpirationText.setTypeface(custom_fonts);

            //
            setDateExpirationText = (TextView) itemView.findViewById(R.id.setDateExpirationText);
            setDateExpirationText.setTypeface(custom_fonts);

            cardview = (CardView) itemView.findViewById(R.id.cardView);
            cardview.setOnClickListener(this);

        }

        @Override
        public void onClick(View v) {
            Context context = itemView.getContext();
            Intent intent = new Intent(context, CardDetailsActivity.class);
            context.startActivity(intent);
        }
    }

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

        View itemView = LayoutInflater.
                from(parent.getContext()).
                inflate(R.layout.item_cardview, parent, false);

        return new CardViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final CardViewHolder holder, final int position) {
        Card Card = cardsList.get(position);
        holder.nameCard.setText(Card.getNameCard());
        holder.dateText.setText(R.string.data_dodania);
        holder.setDateText.setText(Card.getDateCard());
        holder.mImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopupMenu(holder.mImageButton, position);
            }
        });

    }

1 个答案:

答案 0 :(得分:-1)

如果我理解正确,您想要的只是在Activities之间传递数据,则可以使用Bundles执行此操作。

在这里,你回答了同样的问题: How should I communicate between activities?

有关更复杂的数据,请查看此处:How to send an object from one Android Activity to another using Intents?