2

I have a recyclerview with radiobutton,the aim is to have a pop-up(dialogue) appear anytime the user selects the "non-appropriate" radio button, from which options would appear and based on the users selection from those options, a new activity will be opened with the info from the recyclerview.

So far, i have been able to do the dialogue and activities, but where i have an issue is getting the data from the recyclerview into the new activity.

I have read similar threads and none had the answer i need.

Pass Data from Dialog to new Activity

RecyclerView Click to pass data to new activity

Main Activity

////Firebase Adapter and related properties

        FirebaseRecyclerOptions<Drugs> options = new FirebaseRecyclerOptions.Builder<Drugs>()
                .setQuery(query, Drugs.class).build();

        adapter = new FirebaseRecyclerAdapter<Drugs, DrugsViewHolder>(options) {
            @Override
            protected void onBindViewHolder(@NonNull DrugsViewHolder holder, final int position, @NonNull Drugs model) {
                holder.setDiag1(model.getBrand());
                holder.setDiag2(model.getGeneric());
                holder.setDiag3(model.getDose());
                holder.setDiag4(model.getFrequency1(), model.getFrequency2(), model.getDuration());



            }

            @NonNull
            @Override
            public DrugsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

                ////Recycler view item
                View view = LayoutInflater.from(viewGroup.getContext())
                        .inflate(R.layout.drug_display_list,viewGroup,false);///
                return new DrugsViewHolder(view);
            }
        };

        patient_drug_list.setLayoutManager(new LinearLayoutManager(this));
        patient_drug_list.setAdapter(adapter);//// setting adapter
        adapter.notifyDataSetChanged();
        adapter.startListening();
    }

    @Override
    protected void onStart() {
        super.onStart();
        adapter.startListening();
        adapter.notifyDataSetChanged();
    }

    public void rbclick(View view) {
        ////Activated when ever a user presses the "non-appropriate" radiobutton

        AlertDialog.Builder myDialog = new AlertDialog.Builder(Review2.this);
        LayoutInflater inflater = LayoutInflater.from(Review2.this);
        View myview = inflater.inflate(R.layout.adr_options, null);
        myDialog.setView(myview);
        myDialog.show();

    }


////////////////click for new option activities

           //This is a switch for the adio button options that appear after the above radiobutton
    public void clickers(View view) {
        switch (view.getId()){
            case  R.id.drug_needed:
                startActivity(new Intent(getApplicationContext(),Drug_needed.class));
                break;


            case R.id.wrong_drug:
                Toast.makeText(Review2.this, "2nd", Toast.LENGTH_SHORT).show();
                break;

            case R.id.dose_related:
                startActivity(new Intent(getApplicationContext(),Dose.class));
                break;
            case R.id.adverse_reaction:
                startActivity(new Intent(getApplicationContext(),ADR.class));
                break;

            case R.id.food_interaction:
                Toast.makeText(this, "yet to do", Toast.LENGTH_SHORT).show();
                break;

            default:
                Toast.makeText(Review2.this, "nothing to see", Toast.LENGTH_SHORT).show();
                break;
        }
    }


    ////////////////View Holder

    private class DrugsViewHolder extends  RecyclerView.ViewHolder{
        View mView;
        DrugsViewHolder(@NonNull View itemView) {
            super(itemView);
            mView = itemView;



        }



        void setDiag1(String diag1){
            TextView postdiag1 = mView.findViewById(R.id.brand);
            postdiag1.setText(diag1);

        }
        void setDiag2(String diag2){
            TextView postdiag2 = mView.findViewById(R.id.generic);
            postdiag2.setText(diag2);
            postdiag2.setTextSize(12);
        }
        public void setDiag3(String diag3){
            TextView postdiag3 = mView.findViewById(R.id.dose);
            postdiag3.setText("Dose:" + diag3);
        }
        public void setDiag4(String diag4, String diag5, String diag6){
            TextView postdiag4 = mView.findViewById(R.id.frequency);
            postdiag4.setText("Frequency:" + diag4 + " tab " + diag5+"x" + "daily for "+diag6 + "days");
        }
    }

Recyclerview layout(showing only the radiobuttons with onClick function)

    <RadioGroup
        android:id="@+id/radio_appropriate"
        android:layout_below="@+id/view2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <RadioButton
            android:id="@+id/appropraite"
            android:layout_weight="1"
            android:textSize="15sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Appropriate"
            android:textColor="@color/cool_blue"
            android:layout_margin="10dp"/>

        <RadioButton
            android:onClick="rbclick"
            android:id="@+id/not_appropriate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:text="Inapropriate"
            android:textColor="@color/cool_blue" />

    </RadioGroup>

Below is a picture summary of what i want to do

Selecting the "non appropriate" radiobutton

Dialogue after selecting the radiobutton from recyclerview

What should happen(what am expecting)

Any ideas or suggestions or links on how i should go about it would be appreciated.

PhxIT
  • 25
  • 5

3 Answers3

0

The RecyclerView should pass the info into the AlertDialog by simply passing input arguments into the method that displays the AlertDialog. The AlertDialog can then pass that data to the next Activity using Intent extras.

DrugsViewHolder(@NonNull View itemView) {
    super(itemView);
    mView = itemView;
    RadioButton radioButton = mView.findViewById(appropraite);
    radioButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //open AlertDialog
        }
    });


}
Gavin Wright
  • 2,115
  • 2
  • 10
  • 25
  • Would try this and get back to you.Thanks for the quick reply – PhxIT Mar 11 '19 at 12:35
  • Since am using the onclick attribute in my radio button xml file,how do i get the data to pass it into the AlertDialog . The data can only be gotten from the onBindViewHolder method.And i don't want to set an OnClickListener for my Recycler view because, i want only the click of the radio button. – PhxIT Mar 11 '19 at 12:44
  • Don't use the onClick attribute. Just never use it in general. You should set an onClickListener for your RadioButton inside the constructor of the ViewHolder. – Gavin Wright Mar 11 '19 at 12:50
  • i tried that on several occasions, and since its just a layout file(xml) and not an activity, i could not set an onClickListenser on the radio button.If you have any suggestion on how to go about it, kindly let me know. – PhxIT Mar 11 '19 at 13:15
  • your edit worked like a charm, i can't believe i spent so much time on such an issue, just to find a simple solution as this.Bless up bro. – PhxIT Mar 11 '19 at 13:56
  • No problem. Don't forget to formally accept my answer. – Gavin Wright Mar 11 '19 at 14:00
  • I surely will, with the radio button issue out of the way, the only issue that stands now is the OnBindviewHolder and passing the data into the alertDialogue. I only want to know how to get the data from the recyclerView without directly setting on onClickListener on the recyclerview(in the OnBindViewHolder) just to get the position of the selected radiobutton, the rest am sure i am capable of doing . I have a feeling am confused – PhxIT Mar 11 '19 at 14:08
  • You just need to know how to find the position of the current view in the backing array? If you're in the constructor of DrugsViewHolder(), you use `getAdapterPosition()`. If you're in onBindViewHolder(), you just use the `position` parameter that's provided to you as an input parameter. – Gavin Wright Mar 11 '19 at 14:12
  • am new to android as a whole, so, i am not familiar with some of these terms,i will look them on google, and add on to my knowlegde. All i want to do now is get the data after radioButtonclick and pass it to the alertDialog.A code snippet will do, sorry to bother you though, honestly am really confused, thanks for the patience shown. – PhxIT Mar 11 '19 at 14:30
  • This is sort of complicated by the fact that you're using FirebaseRecyclerAdapter, which I've never used before. You'll need to use an interface as shown here: https://stackoverflow.com/a/28304164/7434090 . If you don't like that link, try your luck with another one: https://www.google.com/search?q=firebaseRecyclerAdapter+clicklistener&oq=firebaseRecyclerAdapter+clicklistener&aqs=chrome..69i57.8884j0j1&sourceid=chrome&ie=UTF-8 . But, ultimately, you're supposed to put the ClickListener in the constructor of the ViewHolder and not in onBindViewHolder(). – Gavin Wright Mar 11 '19 at 14:44
0

you did not set extras to intent for example you want to send "this is a sample data" to another activity

//set data int current activity
Intent intent = new Intent(getApplicationContext(),ADR.class);
intent.putExtra("key","this is a sample data");
startActivity(intent);

in ADR activity:

//get data in activity ADR
String newString= extras.getString("key");

i hope this help you

  • Thanks, will try it and get back to you – PhxIT Mar 11 '19 at 12:35
  • the string(putExtra) i need can only be gotten from the OnBindViewHolder.But the radiobutton is part of a layout file(xml) linked to the recycler view.Is there a way i can link the layout file and its contents(radiobutton) to my main activity, in order to use the "set Extra" intent features – PhxIT Mar 11 '19 at 13:25
  • your viewholder should be like this :DrugsViewHolder(@NonNull View itemView) { super(itemView); mView = itemView; RadioButton radioButton = mView.findViewById(appropraite); now you have Radio button object , you can send this object parameters to your activity, if you want to have this object in everywhere you can define it as a static method or if you want, you can use eventbus to send objects to your activity, there is too many solution that you can easily find by searching – keivan shamlu Mar 11 '19 at 13:56
  • your welcome ,you can tick this answer as a true answer for preventing confusion for other users – keivan shamlu Mar 11 '19 at 14:06
0

It's more of a suggestion, have you tried to set the tag of the view with the index in the onCreateViewHolder gets

view.setTag(i); //i is the position that the view in the recycle view

Than in your activitiy onClick functionality you should do

int index = (int)view.getTag();//view that the onClick function is getting

Than with the index that you have you can do something like:

Data currentItem = dataSet.get(index); //get the data you need to pass to the alert dialog 

That's how I usually do it in list view and also in recycle views (although I need to setOnClickListener to the layout of recycle view).

Anton Makov
  • 736
  • 1
  • 7
  • 24