单选按钮按下后将数据从recyclerview传递到新活动

时间:2019-03-11 12:19:15

标签: android android-recyclerview

我有一个带有单选按钮的回收视图,目的是在用户选择“不合适的”单选按钮时弹出一个对话框(对话框),从中出现选项,并基于用户从这些选项中的选择,将会打开一个新活动,其中包含来自recyclerview的信息。

到目前为止,我已经能够进行对话和活动了,但是我遇到的一个问题是将数据从recyclerview导入到新活动中。

我读过类似的主题,没有人得到我需要的答案。

Pass Data from Dialog to new Activity

RecyclerView Click to pass data to new 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布局(仅显示具有onClick功能的单选按钮)

    <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>

下面是我想做什么的图片摘要

Selecting the "non appropriate" radiobutton

Dialogue after selecting the radiobutton from recyclerview

What should happen(what am expecting)

任何有关我应如何进行的想法或建议或链接,将不胜感激。

2 个答案:

答案 0 :(得分:0)

RecyclerView应该通过简单地将输入参数传递到显示AlertDialog的方法中来将信息传递给AlertDialog。然后,AlertDialog可以使用Intent Extras将数据传递到下一个Activity。

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


}

答案 1 :(得分:0)

您没有设置其他目的 例如您要将“这是一个示例数据”发送到另一个活动

grep -o '\<WORD\>' | wc -l

在ADR活动中:

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

我希望这对您有帮助