将对象从一个片段发送到另一个片段
Send Object from One Fragment to Another
我有两个片段,其中第一个片段(业务)有一个对象字符串列表需要传输到第二个片段(业务详细信息)。\
我想知道最好的练习方法是什么,我应该怎么做?
public class Business extends Fragment {
public List<StringList> businessNews = new ArrayList<>();
private RecyclerView recyclerView;
StringList stringList; //object need to transfered to other fragment
public Business() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_business, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.business_recycler_view);
FetchLists f = new FetchLists();
f.execute(10, 0);
return view;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public class FetchLists extends AsyncTask<Integer, Void, List<StringList>> {
@Override
protected List<StringList> doInBackground(Integer... params) {
int count = params[0];
int offset = params[1];
String urlString = "https://nei.org/v1/articlesbjkbknklnmlmerg&sortBy=top&apiKey=50e2bjkbbkba5a5f476ff528a8";
urlString = urlString + "&count=" + count + "&offset=" + offset;
try {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
InputStream stream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
String line = reader.readLine();
String response = "";
while (line != null) {
response += line;
line = reader.readLine();
}
JSONObject object = new JSONObject(response);
JSONArray emailLists = object.getJSONArray("articles");
for (int i = 0; i < emailLists.length(); i++) {
JSONObject listData = (JSONObject) emailLists.get(i);
stringList = new StringList();
stringList.authorName = listData.getString("author");
stringList.headline = listData.getString("title");
stringList.publishedTime = listData.getString("publishedAt");
stringList.newsDetail = listData.getString("description");
businessNews.add(stringList);
Log.d("ashu", "authorname" + stringList.authorName);
}
} catch (Exception e) {
e.printStackTrace();
}
return businessNews;
}
public class BusinessAdapter extends RecyclerView.Adapter<BusinessHolder> {
@Override
public BusinessHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.layout_news, parent, false);
return new BusinessHolder(view);
}
@Override
public void onBindViewHolder(BusinessHolder holder, int position) {
StringList m = c.get(position);
holder.bindListName(m, position);
}}
public class BusinessHolder extends RecyclerView.ViewHolder {
public TextView headlineTextview;
public TextView authorTextview;
public TextView timeTextview;
public BusinessHolder(View itemView) {
super(itemView);
headlineTextview = (TextView) itemView.findViewById(R.id.id_headline);
authorTextview = (TextView) itemView.findViewById(R.id.id_author);
timeTextview = (TextView) itemView.findViewById(R.id.id_time);
}
第二个片段:
在此片段中,我想将对象数据设置为 Textview 参数
public class BusinessDetail extends Fragment {
StringList mstringList;
private TextView headlineSecond;
public TextView authorSecond;
private TextView detailsSecond;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_business_detail, container, false);
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
headlineSecond = (TextView) view.findViewById(R.id.id_headline_second);
authorSecond = (TextView) view.findViewById(R.id.id_author_second);
detailsSecond = (TextView) view.findViewById(R.id.id_details_second);
}}
如果 BusinessDetail 和 Business 是同一个 activity 的子级,您应该在两个片段和 activity 之间提供一个接口。在您的业务片段中,您可以进行此调用(在片段的 onAttach 中或之后):
((MyActivity)getActivity()).showObjectOnBusiness(stringList);
在 MyActivity showObjectOnBusiness 方法中,您应该将对象传递给 BusinessDetail 片段:
Bundle bundle = new Bundle();
bundle.putParcelable(BusinessDetail.OBJECT_KEY, stringList);
new BusinessDetail().setArguments(bundle);
在您的 BusinessDetail 中,您可以通过参数获取您的对象:
Bundle bundle = getArguments();
if (bundle == null || !bundle.containsKey(OBJECT_KEY)) {
throw new IllegalArgumentException("StringList should not be null");
}
StringList stringList = bundle.getParcelable(OBJECT_KEY);
StringList 应该实现 Parcelable。
在另一个片段中保存对片段的引用 - 不好的做法。您可以使用 this 方法,它工作正常。
碎片不应该相互了解。
不是从一个片段转移到另一个片段,而是在 activity 中声明对象列表并让每个片段从那里获取它:
要在 main activity 中声明对象列表和列表的 getter:
public List<StringList> businessNews = new ArrayList<>();
public List<StringList> getObjectList(){
return objectList;
}
然后在Fragments中,可以得到列表:
((MainActivity) getActivity()).getObjectList();
您可以在 onResume() 中进行此调用以确保片段和 Activity 已准备就绪。
为了更正确的解决方案,这部分 ((MainActivity) getActivity())
可以使用接口来实现,以避免强制转换。
您可以通过以下代码实现此目的
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("Key_List", businessNews);
fragmentInstance.setArguments(bundle);
您也可以使用 Serializable,但 serializable 比 parcelable 慢
Bundle bundle = new Bundle();
bundles.putSerializable("Key_List", businessNews);
fragmentInstance.setArguments(bundle);
你可以使用
Event Bus图书馆
它将简化组件之间的通信,
适用于 publisher/subscriber 模式。
我有两个片段,其中第一个片段(业务)有一个对象字符串列表需要传输到第二个片段(业务详细信息)。\
我想知道最好的练习方法是什么,我应该怎么做?
public class Business extends Fragment {
public List<StringList> businessNews = new ArrayList<>();
private RecyclerView recyclerView;
StringList stringList; //object need to transfered to other fragment
public Business() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_business, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.business_recycler_view);
FetchLists f = new FetchLists();
f.execute(10, 0);
return view;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public class FetchLists extends AsyncTask<Integer, Void, List<StringList>> {
@Override
protected List<StringList> doInBackground(Integer... params) {
int count = params[0];
int offset = params[1];
String urlString = "https://nei.org/v1/articlesbjkbknklnmlmerg&sortBy=top&apiKey=50e2bjkbbkba5a5f476ff528a8";
urlString = urlString + "&count=" + count + "&offset=" + offset;
try {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
InputStream stream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
String line = reader.readLine();
String response = "";
while (line != null) {
response += line;
line = reader.readLine();
}
JSONObject object = new JSONObject(response);
JSONArray emailLists = object.getJSONArray("articles");
for (int i = 0; i < emailLists.length(); i++) {
JSONObject listData = (JSONObject) emailLists.get(i);
stringList = new StringList();
stringList.authorName = listData.getString("author");
stringList.headline = listData.getString("title");
stringList.publishedTime = listData.getString("publishedAt");
stringList.newsDetail = listData.getString("description");
businessNews.add(stringList);
Log.d("ashu", "authorname" + stringList.authorName);
}
} catch (Exception e) {
e.printStackTrace();
}
return businessNews;
}
public class BusinessAdapter extends RecyclerView.Adapter<BusinessHolder> {
@Override
public BusinessHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.layout_news, parent, false);
return new BusinessHolder(view);
}
@Override
public void onBindViewHolder(BusinessHolder holder, int position) {
StringList m = c.get(position);
holder.bindListName(m, position);
}}
public class BusinessHolder extends RecyclerView.ViewHolder {
public TextView headlineTextview;
public TextView authorTextview;
public TextView timeTextview;
public BusinessHolder(View itemView) {
super(itemView);
headlineTextview = (TextView) itemView.findViewById(R.id.id_headline);
authorTextview = (TextView) itemView.findViewById(R.id.id_author);
timeTextview = (TextView) itemView.findViewById(R.id.id_time);
}
第二个片段:
在此片段中,我想将对象数据设置为 Textview 参数
public class BusinessDetail extends Fragment {
StringList mstringList;
private TextView headlineSecond;
public TextView authorSecond;
private TextView detailsSecond;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_business_detail, container, false);
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
headlineSecond = (TextView) view.findViewById(R.id.id_headline_second);
authorSecond = (TextView) view.findViewById(R.id.id_author_second);
detailsSecond = (TextView) view.findViewById(R.id.id_details_second);
}}
如果 BusinessDetail 和 Business 是同一个 activity 的子级,您应该在两个片段和 activity 之间提供一个接口。在您的业务片段中,您可以进行此调用(在片段的 onAttach 中或之后):
((MyActivity)getActivity()).showObjectOnBusiness(stringList);
在 MyActivity showObjectOnBusiness 方法中,您应该将对象传递给 BusinessDetail 片段:
Bundle bundle = new Bundle();
bundle.putParcelable(BusinessDetail.OBJECT_KEY, stringList);
new BusinessDetail().setArguments(bundle);
在您的 BusinessDetail 中,您可以通过参数获取您的对象:
Bundle bundle = getArguments();
if (bundle == null || !bundle.containsKey(OBJECT_KEY)) {
throw new IllegalArgumentException("StringList should not be null");
}
StringList stringList = bundle.getParcelable(OBJECT_KEY);
StringList 应该实现 Parcelable。
在另一个片段中保存对片段的引用 - 不好的做法。您可以使用 this 方法,它工作正常。
碎片不应该相互了解。
不是从一个片段转移到另一个片段,而是在 activity 中声明对象列表并让每个片段从那里获取它:
要在 main activity 中声明对象列表和列表的 getter:
public List<StringList> businessNews = new ArrayList<>();
public List<StringList> getObjectList(){
return objectList;
}
然后在Fragments中,可以得到列表:
((MainActivity) getActivity()).getObjectList();
您可以在 onResume() 中进行此调用以确保片段和 Activity 已准备就绪。
为了更正确的解决方案,这部分 ((MainActivity) getActivity())
可以使用接口来实现,以避免强制转换。
您可以通过以下代码实现此目的
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("Key_List", businessNews);
fragmentInstance.setArguments(bundle);
您也可以使用 Serializable,但 serializable 比 parcelable 慢
Bundle bundle = new Bundle();
bundles.putSerializable("Key_List", businessNews);
fragmentInstance.setArguments(bundle);
你可以使用 Event Bus图书馆
它将简化组件之间的通信,
适用于 publisher/subscriber 模式。