在CreateView之后运行方法

时间:2014-04-19 01:00:01

标签: java android multithreading android-fragments

大家好我的片段有问题。 我有一个启动画面,我用AsyncTask设置数组的字符串!没关系!

Splashscreen.java

        @Override
    protected String doInBackground(String... params) {
        try {           
            org.jsoup.nodes.Document doc = Jsoup.connect("http://s.eu").timeout(7*1000).get();

            org.jsoup.nodes.Element tabella = doc.getElementsByClass("tabella-voli").first();
            Iterator<org.jsoup.nodes.Element> iterator = tabella.select("td").iterator();

            while(iterator.hasNext()){
                thread++;

                Compagnia[thread] = iterator.next().text();
                CodiceVolo[thread] = iterator.next().text();
                Citta[thread] = iterator.next().text();
                OraPrevista[thread] = iterator.next().text();
                OraStimata[thread] = iterator.next().text();
                StatoVolo[thread] = iterator.next().text();

                System.out.println("THREAD: "+CodiceVolo[thread]);
            }
        }catch (IOException e) {
            e.printStackTrace();
        }return info;
    }

    @Override
    protected void onPostExecute(String result) { 
        super.onPostExecute(result);
        prog.dismiss();

        finish();
        Intent intent = new Intent(SplashScreen.this, MainActivity.class);
        SplashScreen.this.startActivity(intent);

    //  new Partenze().execute("");

    }
}
在控制台中我可以看到数组的问题,但我有一个片段: 的 arrivi.java

    public class arrivi extends Fragment {

    ListView list;
    List<RowItem> rowItems;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       View arrivi = inflater.inflate(R.layout.arrivi, container, false); 
       return arrivi;
    }

    public void CheckRow(){

        System.out.println("EDEGUITO CHECKROW");

        SplashScreen x = new SplashScreen();
        rowItems = new ArrayList<RowItem>();
        for (int i = 1; i <= x.thread; i++) {

            RowItem item = new RowItem(x.Compagnia[i], x.CodiceVolo[i], x.Citta[i],x.OraPrevista[i],  x.OraStimata[i], x.StatoVolo[i]);
            rowItems.add(item);
            System.out.println("EDEGUITO CHECKROW");

            System.out.println(x.CodiceVolo[i]);
        } 
        list=(ListView)getActivity().findViewById(R.id.listView1);
        CustomBaseAdapter adapter = new CustomBaseAdapter(getActivity(), rowItems);
        list.setAdapter(adapter);
    }
}  

问题是:当我填充数组时,我需要在片段中的createView之后调用CheckRow。 怎么解决? 谢谢

1 个答案:

答案 0 :(得分:0)

你应该在onActivityCreated()中调用它,因为它是在onCreatView()之后调用的。

示例:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    checkRow();
}

这里是片段的生命周期 http://developer.android.com/guide/components/fragments.html