gpt4 book ai didi

java - 将数据从 fragment 传递到适配器的问题

转载 作者:行者123 更新时间:2023-12-02 11:50:31 26 4
gpt4 key购买 nike

我在将数据从 fragment 传递到适配器时遇到问题。

1)我将数据从 Activity 传递到 fragment

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//Set the fragment initially
UominiEDonne fragment = new UominiEDonne();
FragmentTransaction fragmentTransaction =getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();

toolbar = (Toolbar) findViewById(R.id.toolbar);
Intent intent = getIntent();
if(intent!=null){
nome = intent.getStringExtra("nome");
email = intent.getStringExtra("email");
uid= intent.getStringExtra("uid");

UominiEDonne mainFragment = UominiEDonne.newInstance(uid);
}
}

2)我在fragment中获取数据

public static UominiEDonne newInstance(String param1) {
UominiEDonne fragment = new UominiEDonne();
Bundle args = new Bundle();
args.putString("ARG_PARAM1", param1);
fragment.setArguments(args);
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString("ARG_PARAM1");
System.out.println("mParam1"+mParam1);

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


View rootView = inflater.inflate(R.layout.fragment_main, container, false);


db = new SQLiteHandler(getActivity());

getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

mSwipeRefreshLayout = (SwipeRefreshLayout)rootView. findViewById(R.id.swipe_container);
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
new GetVideos().execute();
}
});

videoList = new ArrayList<>();
videoList2 = new ArrayList<>();
System.out.println("ID2:"+idd);

mAdapter = new VideoAdapter(videoList,videoList2, this,this,mParam1);

iv=(ImageView) rootView.findViewById(R.id.shareButton2);


videoRecyclerView = (RecyclerView)rootView. findViewById(R.id.recycler_view);
videoRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
videoRecyclerView.setHasFixedSize(true);

videoRecyclerView.setAdapter(mAdapter);
new GetVideos().execute(part1,part2);
uiHelper = new UiLifecycleHelper(getActivity(), null);
uiHelper.onCreate(savedInstanceState);

// Inflate the layout for this fragment
return rootView;
}

但是如果我将 idd 传递给 adapter 构造函数,则在 onCreate 中 idd 为 null...我不知道为什么!

这是代码 fragment :

public static UominiEDonne newInstance(String param1) {
UominiEDonne fragment = new UominiEDonne();
Bundle args = new Bundle();
args.putString("ARG_PARAM1", param1);
fragment.setArguments(args);
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString("ARG_PARAM1");
System.out.println("mParam1"+mParam1);

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


View rootView = inflater.inflate(R.layout.fragment_main, container, false);


db = new SQLiteHandler(getActivity());

getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

mSwipeRefreshLayout = (SwipeRefreshLayout)rootView. findViewById(R.id.swipe_container);
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
new GetVideos().execute();
}
});

videoList = new ArrayList<>();
videoList2 = new ArrayList<>();
System.out.println("ID2:"+idd);

mAdapter = new VideoAdapter(videoList,videoList2, this,this,mParam1);

iv=(ImageView) rootView.findViewById(R.id.shareButton2);


videoRecyclerView = (RecyclerView)rootView. findViewById(R.id.recycler_view);
videoRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
videoRecyclerView.setHasFixedSize(true);

videoRecyclerView.setAdapter(mAdapter);
new GetVideos().execute(part1,part2);
uiHelper = new UiLifecycleHelper(getActivity(), null);
uiHelper.onCreate(savedInstanceState);

// Inflate the layout for this fragment
return rootView;
}

最佳答案

您不能像这样将参数传递给 fragment 。您需要使构造函数静态。检查下面的代码;

public class BlankFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;



public BlankFragment() {
// Required empty public constructor
}

/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment BlankFragment.
*/
// TODO: Rename and change types and number of parameters
public static BlankFragment newInstance(String param1, String param2) {
BlankFragment fragment = new BlankFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_blank, container, false);
}
}

然后你需要使用该构造函数创建 fragment

BlankFragment fragment = BlankFragment.newInstance("param1","param2");

根据更新的问题进行更新

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
Intent intent = getIntent();
if(intent!=null){
nome = intent.getStringExtra("nome");
email = intent.getStringExtra("email");
uid= intent.getStringExtra("uid");
UominiEDonne mainFragment = UominiEDonne.newInstance(uid);
FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, mainFragment);
fragmentTransaction.commit();
}
}

关于java - 将数据从 fragment 传递到适配器的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47902466/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com