我需要迭代數組並將每個數組項從活動傳遞到片段;但與我的代碼,我傳遞只從最後一項活動片段。問題將數據從活動傳遞到片段
我希望你能幫助我!
我向你展示我的代碼!
活動:
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