0

I have ActivityCourses and ActivityNewCourse. ActivityCourses include a list (recyclerview) of custom courses that user make, and ActivityNewCourse obviously is the activity where the user makes those custom courses.

ActivityNewCourse contains primitive data (course name and number of holes) those are the data, which I pass to ActivityCourses, and make new item there in its recyclerview (so the new items name is that course name which is passed from ActivityNewCourse).

The problem I have is that ActivityNewCourse also contains recyclerview, and that contains obviously unique items. I need to get all those items, from ActivityNewCourse, and get them STORED (not shown) in that same item, where I send primitive data in ActivityCourses.

I've tried to use interface in my NewCourseAdapter, to pass those items from ActivityNewCourse recyclerview, to my ActivityCourses item, but the problem is that I need all of them items, not just 1. and also that button "Save Course" which user clicks to obviously save all the data from ActivityNewCourse, is outside of the recyclerview, where those items are located.

HERE IS MY NEW COURSE ADAPTER

public class NewCourseAdapter extends RecyclerView.Adapter<NewCourseAdapter.NewCourseViewHolder> {
    private ArrayList<NewCourseItem> mNewCourseList;
    private OnItemClickListener mListener;

    public interface OnItemClickListener {
        void onMinusClick(int position);
        void onPlusClick(int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mListener = listener;
    }

    public static class NewCourseViewHolder extends RecyclerView.ViewHolder {
        public TextView mTextView1, mTextView2;
        public ImageView mImageView1, mImageView2;

        public NewCourseViewHolder(@NonNull View itemView, final OnItemClickListener listener) {
            super(itemView);
            mTextView1 = itemView.findViewById(R.id.hole_number);
            mTextView2 = itemView.findViewById(R.id.par_number);
            mImageView1 = itemView.findViewById(R.id.item_minus_btn);
            mImageView2 = itemView.findViewById(R.id.item_plus_btn);

            mImageView1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (listener != null) {
                        int position = getAdapterPosition();

                        if (position != RecyclerView.NO_POSITION) {
                            listener.onMinusClick(position);
                        }
                    }
                }
            });

            mImageView2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (listener != null) {
                        int position = getAdapterPosition();

                        if (position != RecyclerView.NO_POSITION) {
                            listener.onPlusClick(position);
                        }
                    }
                }
            });
        }
    }

    public NewCourseAdapter(ArrayList<NewCourseItem> newCourseList) {
        mNewCourseList = newCourseList;
    }

    @NonNull
    @Override
    public NewCourseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.new_course_item, parent, false);
        NewCourseViewHolder evh = new NewCourseViewHolder(v, mListener);
        return evh;
    }

    @Override
    public void onBindViewHolder(@NonNull NewCourseViewHolder holder, int position) {
        NewCourseItem currentItem = mNewCourseList.get(position);

        holder.mTextView1.setText(currentItem.getText1());
        holder.mTextView2.setText(currentItem.getText2());
        holder.mImageView1.setImageResource(currentItem.getImageMinus());
        holder.mImageView2.setImageResource(currentItem.getImagePlus());
    }

    @Override
    public int getItemCount() {
        return mNewCourseList.size();
    }
}

HERES HOW I PASS PRIMITIVE DATA FROM ACTIVITYNEWCOURSE TO ACTIVITYCOURSES

save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            courseName = findViewById(R.id.course_name_input);
            number = findViewById(R.id.number_of_holes_number);
            String intentCourseName = courseName.getText().toString().trim();
            String holeNumber = number.getText().toString().trim();

            Intent intent = new Intent(ActivityNewCourse.this, ActivityCourses.class);
            intent.putExtra("COURSENAME", intentCourseName);
            intent.putExtra("HOLENUMBER", holeNumber);
            startActivity(intent);
        }
    });

HERES HOW ACTIVITYCOURSES RESIVE THE DATA

public void addItem() {
    if (getIntent().getStringExtra("COURSENAME") != null) {
        mCourselist.add(new CoursesItem(getIntent().getStringExtra("COURSENAME"), "Holes:", getIntent().getStringExtra("HOLENUMBER"), R.drawable.ic_delete));
    }
}

I'm not sure if this code helped or not... Am I somehow be able to send the whole arraylist from ActivityNewCourse, when save button been clicked? I don't know, I'm kinda dead end and I have no clue what to do, so any suggestions on what to do in this situation would help...

Vadim Kotov
  • 7,103
  • 8
  • 44
  • 57
Antti
  • 59
  • 1
  • 5

1 Answers1

0

Have you tried using intent.putStringArrayListExtra("ARRAYNAME", arrayName); try putting your data in an ArrayList and send it like this:

Intent intent = new Intent(ActivityNewCourse.this, ActivityCourses.class);
intent.putStringArrayListExtra("ARRAYNAME", arrayName);
startActivity(intent);

and the retrieve it like this:

ArrayList<String> listOfData = getIntent().getStringArrayListExtra("ARRAYNAME");

Hope it helps.

updated

Using Serializable:

first extend NewCourseItem to Serializable then:

Bundle bundle = new Bundle();
bundle.putSerializable("KEYNAME", new NewCourseItem("str1", "str2", "imglink", "imglink"));
intent.putExtras(bundle);
startActivity(intent);

retrieve it like this:

Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
NewCourseItem item = (NewCourseItem)bundle.getSerializable("KEYNAME");
  • I have 2 Strings and 2 ImageViews, in the item. I really only need that String data, so can I parse/ignore those imageViews out or how should I handle that? – Antti Jul 04 '19 at 16:45
  • okay, do you want the `NewCourseItem` data (2 strings & 2 imgViews) to be sent to the `ActivityCourses` activity? – Tobibur Rahman Jul 04 '19 at 17:03
  • In ActivityNewCourse, user give course name. That course name I intent to ActivityCourses, where it generates new item to the list, and that item name is that course name, the user just gave. But also I want to send all those NewCourseItems to that same item that course name generated, but only store them in it, so that later on when I generate a new game and call that course name I can use those items which it includes. But like I said, I only REALLY need those Strings from that item, so it won't be mandatory to get those ImageViews, but it won't harm either if they come with it. – Antti Jul 04 '19 at 17:14
  • Ok, I understood your problem now. You need to use Serializable for that. First, extend the `NewCourseItem` POJO to `Serializable` and then get the `NewCourseItem` object from the user and send it through intent using `bundle.putSerializable("KEYNAME", courseItemObj);` here's a link on how to do it: https://stackoverflow.com/a/14333555/8596924 and I can edit my answer too if this is the case. – Tobibur Rahman Jul 04 '19 at 17:25
  • Does that include only 1 item or all of them? – Antti Jul 04 '19 at 17:30
  • All the item of one object of `NewCourseItem` – Tobibur Rahman Jul 04 '19 at 17:35
  • So I have to loop to get all items? – Antti Jul 04 '19 at 17:49
  • Yes, loop to get multiple items and put them in an ArrayList and send it. Check this https://stackoverflow.com/a/21250407/8596924 – Tobibur Rahman Jul 04 '19 at 18:00
  • I think I have it now. How I can check my items in android studio? Just want to see items in ActivityCourses to see if they really contain that arraylist now. – Antti Jul 05 '19 at 04:44
  • I did like this: Bundle bundle = new Bundle(); for (int i = 0; i < mNewCourseList.size(); i++) { bundle.putSerializable("ITEMS" + i, mNewCourseList.get(i)); } intent.putExtras(bundle); RESIVE DATA: Bundle bundle = new Bundle(); ArrayList courseItems = new ArrayList<>(); for (String key : bundle.keySet()) { courseItems.add((CoursesItem) bundle.getSerializable(key)); } DOES THIS WORK? – Antti Jul 05 '19 at 05:36
  • oh wait, when I receive data in ActivityCourses (which use CoursesItem instead of NewCourseItem) should I still call it like this------> Bundle bundle = new Bundle(); ArrayList courseItems = new ArrayList<>(); for (String key : bundle.keySet()) { courseItems.add((NewCourseItem) bundle.getSerializable(key)); } ------> OR that way I said earlier (using CourseItem)? – Antti Jul 05 '19 at 06:55
  • Try both whichever works for you. Sorry for the late reply I'm at work actually. I can look properly into this in the evening. – Tobibur Rahman Jul 05 '19 at 07:31
  • Np m8. I made those codes like in this example in the first link u gave stackoverflow.com/a/14333555/8596924 (that which has 6 upvotes.) now I made test TextView, in ActivityCourses, and try to implement as shown in that example "String txt = courseItems.get(1).getText1();" and tried to set that text (which getText1()) include, to that test TextView that I made (just to see that it really contain that string user gave), but OFC app crashed, don't know why tho... – Antti Jul 05 '19 at 08:26
  • I wonder could the problem be in that for loop "String key" cause my item has 2 strings and 2 images and images are int type... or does that pick only those 2 strings or how that work? – Antti Jul 05 '19 at 14:36
  • do like this https://stackoverflow.com/a/21250407/8596924 (accepted answer) where instead of String change it to `NewCourseItem`. Then it will have no issue with ints because it'll be in the NewCourseItem object already both string and int. – Tobibur Rahman Jul 05 '19 at 17:59
  • Nice, I'm glad I could help. :) – Tobibur Rahman Jul 06 '19 at 02:43