-2

I am trying to make cards in a cardView like Duolingo, I found code online and I am trying to edit that code according to my needs. The issue is I do not know how to use the array list of String and change its text in each card as it changes. I want the text to change in each Card according to the array list that I have created. The issue is I am unable to get "position" from the array to textview and setText to it. Below is my code of the Card Fragment.

public class CardFragment extends Fragment {

    private CardView cardView;

    public static Fragment getInstance(int position) {
        CardFragment f = new CardFragment();
        Bundle args = new Bundle();
        args.putInt("position", position);
        f.setArguments(args);

        return f;
    }


    public static String[] line1 ={
            "Card no 1",
            "Card no 2",
            "Card no 3",
            "Card no 4",
            "Card no 5",
            "Card no 6",

    };

    public static String[] line2 = {
            "This is card number 1",
            "This is card number 2",
            "This is card number 3",
            "This is card number 4",
            "This is card number 5",
            "This is card number 6",
    };

    @SuppressLint("DefaultLocale")
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.item_viewpager, container, false);

        cardView = (CardView) view.findViewById(R.id.cardView);
        cardView.setMaxCardElevation(cardView.getCardElevation() * CardAdapter.MAX_ELEVATION_FACTOR);

        TextView title = (TextView) view.findViewById(R.id.title);
        Button button = (Button)view.findViewById(R.id.button);
        TextView description = (TextView) view.findViewById(R.id.description);

        // how to setText here from the above array list.
        title.setText(line1[position]) // this is giving me error.



        title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "This is Card no " + getArguments().getInt("position"), Toast.LENGTH_SHORT).show();
            }
        });



        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getActivity(), "Button in Card " + getArguments().getInt("position")
                        + "Clicked!", Toast.LENGTH_SHORT).show();
            }
        });

        return view;
    }

    public CardView getCardView() {
        return cardView;
    }


}
prashant17
  • 1,520
  • 3
  • 14
  • 23
sulli110
  • 145
  • 2
  • 16

1 Answers1

1

In your code, you have not set and assign int position variable, so please add below code on above line in which you are getting error

int position = getArguments().getInt("position");

// how to setText here from the above array list.
title.setText(line1[position]) // this is giving me error.
Nirav Bhavsar
  • 2,133
  • 2
  • 20
  • 24