传递数据从活动到另一个,然后传递到片段在ViewPager



我正试图将数据从我的MainActivty传递到另一个ViewPager,并在Fragment中获得该数据。

我尝试了很多方法,但每次应用程序都崩溃了。

下面是我的Classes:

我的持有人:

@Override
public void onBindViewHolder(main_rv_holder holder, int position) {
  holder.name.setText(mains.get(position).getName());
  holder.image_main.setImageResource(mains.get(position).getImg());
  //Clicked
  holder.setItemClickListener(new ItemClickListener() {
    @Override
    public void onItemClick(View view, int position) {
      Intent intent = new Intent(context,ViewerActivity.class);
      intent.putExtra("title",mains.get(position).getName());
      intent.putExtra("pos",position);
      context.startActivity(intent);
    }
  });
}

My Activity with pager:

protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_viewer);
  //Toolbar Initialization
  InitializeToolbar();
  //ViewPAGER Initialization
  InitializePager();
  //Floating Action Button
  Fabinitialize();
  //pass data
  Bundle data = new Bundle();
  //Use bundle to pass data
  Form1 fragmentobj = new Form1();
  data.putString("tag","Hello");
  fragmentobj.setArguments(data);
}

ViewPager中的Fragment:

public class Form1 extends Fragment {
  String TAG;
  public Form1() {
    TAG = getArguments().getString("tag");
    // Required empty public constructor
  }
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment, container, false);
    TextView textView = (TextView) view.findViewById(R.id.text_VIew);
    textView.setText(TAG);
    return view;
  }
}

谢谢。

见下面的代码

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment, container, false);
        TextView textView = (TextView) view.findViewById(R.id.text_VIew);
    String title= getActivity().getIntent().getStringExtra("title");
        textView.setText(title);
            return view;
        }
    }

相关内容

  • 没有找到相关文章

最新更新