将数据从列表片段传递到新活动

时间:2018-01-13 08:48:47

标签: android android-fragments

尝试将活动中的列表片段中的数据传递给新活动并获取null ...

这是我的recyclerview适配器代码......

package com.doctorapp.doctor;

import android.app.LauncherActivity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.Toast;
import android.util.Log;

import com.doctorapp.doctor.MyPatientsFragment.OnListFragmentInteractionListener;
import com.doctorapp.doctor.dummy.DummyContent.DummyItem;

import java.util.List;

/**
 * {@link RecyclerView.Adapter} that can display a {@link DummyItem} and makes a call to the
 * specified {@link OnListFragmentInteractionListener}.
 * TODO: Replace the implementation with code for your data type.
 */
public class MypatientsRecyclerViewAdapter extends RecyclerView.Adapter<MypatientsRecyclerViewAdapter.ViewHolder> {

    private final List<MyPatientsItem> mValues;
    private final OnListFragmentInteractionListener mListener;
    private Context context;

    public MypatientsRecyclerViewAdapter(List<MyPatientsItem> items, OnListFragmentInteractionListener listener) {
        mValues = items;
        mListener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.fragment_patients, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {

        holder.mypatientsItem = mValues.get(position);
        holder.txtPatientName.setText(mValues.get(position).getPatient_name());

        holder.mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int patient_id = mValues.get(position).getPatient_id();
                Toast.makeText(v.getContext(), "My Position : "+ patient_id, Toast.LENGTH_LONG).show();
                Intent i = new Intent(context, PatientProfile.class);
                i.putExtra("patient_id", patient_id);
                context.startActivity(i);
                if (null != mListener) {
                    //Toast.makeText(v.getContext(), "Am clicked.hey", Toast.LENGTH_LONG).show();
                    // Notify the active callbacks interface (the activity, if the
                    // fragment is attached to one) that an item has been selected.
                    mListener.onListFragmentInteraction(holder.mypatientsItem);
                    //Toast.makeText(v.getContext(), "Am clicked.", Toast.LENGTH_LONG).show();

                }
            }
        });
    }


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

    public class ViewHolder extends RecyclerView.ViewHolder {
        public final View mView;
        public final TextView txtPatientId;
        public final TextView txtPatientName;
        public MyPatientsItem mypatientsItem;

        public ViewHolder(View view) {
            super(view);
            context = view.getContext();
            mView = view;
            txtPatientId = (TextView) view.findViewById(R.id.patient_id);
            txtPatientName = (TextView) view.findViewById(R.id.patient_name);
        }

        @Override
        public String toString() {
            return super.toString() + " '" + txtPatientName.getText() + "'";
        }
    }
}

列表填写正常,当我点击它时,还给我带有正确ID的Toast,需要传递,以便下一个活动可以为正确的患者填充数据。在我想要获取数据的第二个活动中,它在我尝试的所有方式中都是null ...

这是代码......

   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_patient_profile);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        //String patient_id_str = (String) savedInstanceState.getSerializable("patient_id");

        Intent intent = getIntent();
        String patient_id_str = intent.getStringExtra("patient_id");

        Toast.makeText(getApplicationContext(), "Load Patient : " + patient_id_str, Toast.LENGTH_LONG).show();


        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

它总是让我归零...我是Android的新手和在线教程学习的东西,所以我很难理解为什么它没有发生,因为互联网上的许多帖子说这应该有效。请告诉我错误的地方。

3 个答案:

答案 0 :(得分:3)

在intent extras中,您要添加整数值,因此您需要在目标活动中获得相同的内容。

检查出来:

int patient_id_str = intent.getIntExtra("patient_id");

答案 1 :(得分:0)

通过Intent

发送数据时,您的工作非常完美
int patient_id = mValues.get(position).getPatient_id();
Intent i = new Intent(context, PatientProfile.class);
i.putExtra("patient_id", patient_id);
context.startActivity(i);

但在接收数据时,您必须通过Bundle

Bundle extras = getIntent().getExtras();
String patient_id_atr = (String) extras.get("patient_id");

答案 2 :(得分:0)

不要使用上下文使用getActivity;

并且putExt需要setFlag