onCreateView在使用FragmentTransaction传递数据之前和之后调用了2次

时间:2013-03-09 10:07:59

标签: android android-asynctask android-fragments

活动代码。我想通过ListFragment将数据传递给FragmentTransaction

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();
            }

        }

    }

FragmentTransaction工作前如何处理asynctask
因为AsyncTaskoncreateview的调用早于fragmentTransaction。在fragmentTransaction之后,oncreateview再次呼叫。并且oncreateview调用了2次

logcat 屏幕截图:http://i.stack.imgur.com/4NKBL.png

1 个答案:

答案 0 :(得分:0)

onActivityCreated

之后调用onCreateView内的调用asynctask
@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
}