简体   繁体   English

在Recycler View中添加和显示数据

[英]add and display data in Recycler View

I have a problem so I have a problem. 我有一个问题,所以我有一个问题。

enter image description here In the Scan Activity, QR Codes are scanned and the card data is generated, then when the value is matched, we go to the next activity where we are shown the next value. 在“扫描活动”中输入图像描述 ,扫描QR码并生成卡数据,然后在匹配值时转到下一个活动,在该活动中显示下一个值。 Click on the Next button and go to our list of data. 单击下一步按钮,然后转到我们的数据列表。 Ask how to get from the ScanActivity.class Activity card data and add a list 询问如何从ScanActivity.class活动卡数据中获取并添加列表

piece of code from ScanActitivty.java 来自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 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 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);

    }

class Class 班级

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 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);
            }
        });

    }

If I understood correctly, and all you want is pass data between Activities , you can do it with Bundles . 如果我理解正确,并且只想在Activities之间传递数据,则可以使用Bundles

Here you have the same questions answered: How should I communicate between activities? 在这里,您有相同的问题得到回答: 我应该如何在活动之间进行沟通?

For more sophisticated data, have a look here instead: How to send an object from one Android Activity to another using Intents? 要获取更复杂的数据,请改为查看: 如何使用Intent将对象从一个Android Activity发送到另一个对象?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM