來自活動的代碼。我想將數據傳遞到ListFragment
與FragmentTransaction
onCreateView在使用FragmentTransaction傳遞數據之前和之後調用2次
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menudisplay);
Bundle extras = getIntent().getExtras();
language = extras.getString("language");
lang_id = extras.getInt("id");
TextView tv = (TextView) findViewById(R.id.tv_lang_use);
//Start passing data
MyListFragment mylist = new MyListFragment();
FragmentTransaction fragmentTrans = getFragmentManager()
.beginTransaction();
mylist.language = language;
fragmentTrans.add(mylist, "language");
Log.d("tes", language +"abc");
fragmentTrans.commit();
}
這是我的片段
編輯(onActivityCreated):
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_menulist_overview, container,
false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
Log.d("Test","How many called?");
new loadcat().execute();
}
public View setUI() {
values = new ArrayList<String>();
try {
if (json.getInt(TAG_SUCCESS) == 1) {
System.out.println("1");
menuresto = json.getJSONArray(TAG_MENU);
String category;
for (int i = 0; i < menuresto.length(); i++) {
System.out.println(menuresto.length());
JSONObject c = menuresto.getJSONObject(i);
// Toast.makeText(getActivity(), c+"",
// Toast.LENGTH_LONG).show();
category = c.getString(TAG_CATEGORY);
values.add(category);
System.out.println(values.get(i));
}
}
} catch (Throwable t) {
Log.e("MyActivity", t.getMessage(), t);
}
updateDetail(values.get(0), values.get(0));
ListView lv = (ListView) view.findViewById(R.id.categoryList);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, values);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int pos,
long id) {
// TODO Auto-generated method stub
int index = pos + 1;
Toast.makeText(getActivity(), "Click ListItem Number " + index,
Toast.LENGTH_LONG).show();
updateDetail(values.get(pos), values.get(pos));
}
});
return view;
}
public void load() {
String langs = language;
System.out.println("a"+langs+"a");
List<NameValuePair> params = new ArrayList<NameValuePair>();
// Log.d("language load", language);
params.add(new BasicNameValuePair("language", langs));
System.out.println("language = " + langs);
String url = url1;
json = jParser.makeHttpRequest(url, "GET", params);
// ArrayList<String> tes = new ArrayList<String>();
// String[] tes = new String[]{};
Log.d("All Category: ", json.toString());
}
,這是我的AsyncTask類:
class loadcat extends AsyncTask<String, Void, Object> {
@Override
protected Object doInBackground(String... params) {
// TODO Auto-generated method stub
load();
return null;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pd = new ProgressDialog(getActivity());
pd.setMessage("tes");
pd.show();
}
@Override
protected void onPostExecute(Object result) {
// TODO Auto-generated method stub
// super.onPostExecute(result);
if (pd != null) {
pd.dismiss();
setUI();
}
}
}
如何在asynctask
工作之前處理FragmentTransaction
?
因爲AsyncTask
在oncreateview
之前調用fragmentTransaction
。在fragmentTransaction
之後,再次調用oncreateview
。和oncreateview
稱爲2倍
logcat的截圖:http://i.stack.imgur.com/4NKBL.png
有我的回答解決你的問題 – DjHacktorReborn 2013-03-09 10:17:42
@DjHacktorReborn:不工作。其他解決方案? – smithonaction 2013-03-09 10:23:21
在onActivityCreate中執行所有初始化,而不是在創建,並在最後運行asynctask,將工作 – DjHacktorReborn 2013-03-09 10:26:00