Java 将数据从活动传递到片段的问题
我需要迭代数组并将数组的每个项从活动传递到片段;但在我的代码中,我只将最后一项传递给活动片段 我希望你能帮助我 我给你看我的代码 活动:Java 将数据从活动传递到片段的问题,java,android,Java,Android,我需要迭代数组并将数组的每个项从活动传递到片段;但在我的代码中,我只将最后一项传递给活动片段 我希望你能帮助我 我给你看我的代码 活动: try { JSONObject contacts = new JSONObject(jsonStr); JSONArray jsonArray = contacts.getJSONArray("pdfs"); // looping thro
try {
JSONObject contacts = new JSONObject(jsonStr);
JSONArray jsonArray = contacts.getJSONArray("pdfs");
// looping through All Contacts
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject c = jsonArray.getJSONObject(i);
// title_array.add(c.getString("title").toString());
url_pdf=c.getString("url_pdf");
SixFragment fragment = new SixFragment();
fragment.setName(url_pdf);
/* Bundle bundle = new Bundle();
bundle.putString("user", url_pdf);
SixFragment fragmentt = new SixFragment();
fragmentt.setArguments(bundle);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.ah, fragmentt)
.commit();
*/
}
System.out.println("PDFSSSSSSS"+url_pdf);
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
});
}
试试看{
JSONObject contacts=新的JSONObject(jsonStr);
JSONArray JSONArray=contacts.getJSONArray(“PDF”);
//通过所有触点循环
for(int i=0;i
片段:
public class SixFragment extends android.support.v4.app.Fragment {
String url_pdf="";
SimpleAdapter adapter;
ListView list;
ArrayList<HashMap<String, String>> pdfList;
HashMap<String, String> pdf;
String data="";
String arg="";
static String azzzz="";
public SixFragment() {
// Required empty public constructor
}
public void setName(String string){
azzzz = string;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_six, container, false);
/*Bundle bundle=getArguments();
String pala=bundle.getString("user");*/
pdfList = new ArrayList<>();
HashMap<String, String> pdf = new HashMap<>();
// adding each child node to HashMap key => value
pdf.put("id", azzzz);
// adding contact to contact list
pdfList.add(pdf);
for (int i = 0; i < pdfList.size(); i++) {
System.out.println("CONTAT:"+pdfList.size());
}
list = (ListView) view.findViewById(R.id.listView);
ListAdapter adapter = new SimpleAdapter(
getContext(), pdfList,
R.layout.list_item, new String[]{"id"}, new int[]{R.id.name});
list.setAdapter(adapter);
return view;
}
public类SixFragment扩展了android.support.v4.app.Fragment{
字符串url_pdf=“”;
SimpleAdapter适配器;
列表视图列表;
ArrayList-pdfList;
HashMap pdf;
字符串数据=”;
字符串arg=“”;
静态字符串azzzz=“”;
公营机构({
//必需的空公共构造函数
}
公共void集合名(字符串){
AZZZ=字符串;
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图=充气机。充气(R.layout.fragment_-six,容器,错误);
/*Bundle=getArguments();
String pala=bundle.getString(“用户”)*/
pdfList=newarraylist();
HashMap pdf=新的HashMap();
//将每个子节点添加到HashMap key=>value
pdf.put(“id”,azzzz);
//将联系人添加到联系人列表
pdfList.add(pdf);
对于(int i=0;i
SIX_FRAGMENT.XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container_frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="info.androidhive.materialtabs.fragments.SixFragment">
<fragment
android:id="@+id/ah"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
<ListView
android:layout_width="match_parent"
android:id="@+id/listView"
android:layout_height="wrap_content"/>
</RelativeLayout>
您将url\u pdf设置为一次又一次的分段,但url\u pdf始终只保留1个值。您应该创建一个数组或JsonObject,并将其作为额外值传递 活动
try {
JSONObject contacts = new JSONObject(jsonStr);
JSONArray jsonArray = contacts.getJSONArray("pdfs");
Bundle bundle = new Bundle();
bundle.putString("user", list);
SixFragment fragmentt = new SixFragment();
fragmentt.setArguments(bundle);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.ah, fragmentt)
.commit();
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
});
}
片段:
public class SixFragment extends android.support.v4.app.Fragment {
ArrayList<String> list;
...
public SixFragment getInstance(JSONArray json) {
Bunble b = new Bundle();
b.putExtra("JSON",json);
SixFragment f = new SixFragment();
f.setArguments(b);
return f;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
savedInstance = savedInstanceState != null;
super.onCreate(savedInstanceState);
JSONArray jsonArray = getArguments.get("JSON");
// looping through All Contacts
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject c = jsonArray.getJSONObject(i);
list.add(c.getString("url_pdf"));
System.out.println("PDFSSSSSSS"+url_pdf);
}
}
public类SixFragment扩展了android.support.v4.app.Fragment{
数组列表;
...
公共六片段getInstance(JSONArray json){
Bunble b=新Bundle();
b、 putExtra(“JSON”,JSON);
SixFragment f=新的SixFragment();
f、 设置参数(b);
返回f;
}
@凌驾
创建时的公共void(@Nullable Bundle savedInstanceState){
savedInstance=savedInstanceState!=null;
super.onCreate(savedInstanceState);
JSONArray JSONArray=getArguments.get(“JSON”);
//通过所有触点循环
for(int i=0;i
注意:您可以使用Gson而不是手动解析JSON
在活动中
try {
// Instantiate a new ArrayList to store the urls
ArrayList<String> urls = new ArrayList<>();
JSONObject contacts = new JSONObject(jsonStr);
JSONArray jsonArray = contacts.getJSONArray("pdfs");
// looping through All Contacts
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject c = jsonArray.getJSONObject(i);
String url_pdf = c.getString("url_pdf");
// add the url to the array of 'url_pdf'
urls.add(url_pdf);
// Display each url retrieved (not necessary)
System.out.println("PDFSSSSSSS" + url_pdf);
}
SixFragment fragment = SixFragment.newInstance(urls);
getSupportFragmentManager().beginTransaction()
// Replace the current Fragment. Use the Fragment class name as tag (can
// be useful to find the fragment later)
.replace(R.id.ah, fragment, fragment.getClass().getSimpleName())
.commit();
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
});
}
试试看{
//实例化一个新的ArrayList来存储URL
ArrayList URL=新的ArrayList();
JSONObject contacts=新的JSONObject(jsonStr);
JSONArray JSONArray=contacts.getJSONArray(“PDF”);
//通过所有触点循环
for(int i=0;i
在六个片段中
private static final String ARGS_URLS = "bundle_urls";
private ArrayList<String> mUrls;
private ArrayAdapter<String> mAdapter;
public SixFragment() {
// Required empty public constructor
}
/**
* @param urls ArrayList of url_pdf
* @return A new instance of the {@link SixFragment}
*/
public static SixFragment newInstance(ArrayList<String> urls) {
Bundle args = new Bundle();
// put the array into the fragment arguments bundle
args.putStringArrayList(ARGS_URLS, urls);
SixFragment fragment = new SixFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Retrieve data from the Fragment's arguments
mUrls = getArguments().getStringArrayList(ARGS_URLS);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_six, container, false);
ListView listView = (ListView) view.findViewById(R.id.listView);
// Use an ArrayAdapter instead of the SimpleAdapter as it automatically manage a List
mAdapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1, mUrls);
listView.setAdapter(mAdapter);
return view;
}
private静态最终字符串ARGS\U