我需要迭代数组并将每个数组项从活动传递到片段;但与我的代码,我传递只从最后一项活动片段。问题将数据从活动传递到片段
我希望你能帮助我!
我向你展示我的代码!
活动:
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();
}
});
}
片断:
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;
}
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>
你应该让你的数据在一个数组,然后创建您的片段,并通过阵列中的片段参数,所以Fragment可以在创建时获得它们。或者你创建你的Fragment,获取数组中的数据(在Activity中),然后用数组更新Fragment(通过在你的Fragment上调用“update”方法) 但是你现在正在做的是创建您的“for”每个循环中的碎片! – Eselfar
@Eselfar你能举个例子吗? – Markus
我在下面添加了一个例子。 – Eselfar