将数据传递给一个活动到片段

时间:2017-03-16 11:21:32

标签: java android android-fragments android-intent

很抱歉询问重复的问题,但在检查其他问题后,我使用他们的答案他们不能在我的代码中工作我是新手android

以下是我要传递数据的类

public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder>   {


    private int[] mdataset;
    private String[] mplanet;

    Context c;
    public MainAdapter(Context context ,int[] dataset , String[] planet){

        this.c = context;
        this.mplanet = planet;
        this.mdataset = dataset;

    }

    @Override
    public void onBindViewHolder(MainAdapter.ViewHolder holder, final int position ) {
        holder.image.setImageResource(mdataset[position]);
        holder.textView.setText(mplanet[position]);

        holder.image.setOnClickListener(new View.OnClickListener() {
            @Override

            public void onClick(View v) {


      //   here is my i want to pass the data position to another fragment** 


                Bundle bundle = new Bundle();
                bundle.putInt("position", position);
                bottomActivity fragment = new bottomActivity();
                fragment.setArguments(bundle);

            }
        });
    }

    @Override
    public int getItemCount() {
        return mdataset.length;
    }




    public class ViewHolder extends RecyclerView.ViewHolder {

        ImageView image;
        TextView textView;

        public ViewHolder(View itemView) {
            super(itemView);

            image = (ImageView) itemView.findViewById(R.id.imageView);
            textView  = (TextView)itemView.findViewById(R.id.textView);

        }
    }


    @Override
    public MainAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


           View  v = LayoutInflater.from(parent.getContext())
                   .inflate(R.layout.single_row, null);

        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

}

这是我的Fragment课程,我想要接收数据

public class bottomActivity extends Fragment
 {

    TextView textView;
    int pos;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view =  inflater.inflate(R.layout.fragment_bottom, container, false);

        textView = (TextView)view.findViewById(R.id.title_textView);

        textView.setText(pos+"");
        return  view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);


//  here is that code where the data should received 

        Bundle bundle = bottomActivity.this.getArguments();

        if (bundle != null) 
        {

            pos = bundle.getInt("position", 2);

        }

    }
}

3 个答案:

答案 0 :(得分:1)

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view =  inflater.inflate(R.layout.fragment_bottom, container, false);

        textView = (TextView)view.findViewById(R.id.title_textView);
        // get data from the bundle you sent
        int position=getArguments().getInt("position");

        textView.setText(position+"");
        return  view;
    }

在片段的onCreatView中,这样做

答案 1 :(得分:0)

最好的方法是在片段中使用静态newInstance方法,并发送所需的所有数据,如参数。下一步是片段中的onCreate方法获取您传递的所有数据。

e.g

活动

YourFragment fragment = YourFragment.newInstance(10, 4);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(id, fragment).commit();

片段

int data1, data2;
 public static YourFragment newInstance(int data1, int data2) {
    YourFragment fragment = new YourFragment();
    Bundle args = new Bundle();
    args.putInt("data1", data1);
    args.putInt("data2", data2);
    fragment.setArguments(args);
    return fragment;
}


@Override
public void onCreate(Bundle savedInstanceState) {
    data1 = getArguments().getInt("data1", 0);
    data2 = getArguments().getInt("data2", 0);

    super.onCreate(savedInstanceState);
}

我认为你需要这种情况,在创建片段之前发送数据。

干杯:)

答案 2 :(得分:0)

在适配器类

中的onClick方法中使用它
public void onClick(View v) {
    Bundle bundle = new Bundle();
    bundle.putInt("position", position);           
    bottomActivity fragment = new bottomActivity ();
    fragment.setArguments(bundle);
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.add(CONTENT_VIEW_ID, fragment).commit();
   }

其中CONTENT_VIEW_ID是应在活动类中定义的容器

然后在bottomActivity

中的onCreateView片段类中
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view =  inflater.inflate(R.layout.fragment_bottom, container,         false);
        Bundle bundle = this.getArguments();
        if (bundle != null) 
        {
            pos = bundle.getInt("position", 2);
        }
        textView = (TextView)view.findViewById(R.id.title_textView);
        textView.setText(pos+"");
        return  view;
    }