将字符串值从asynctask传递给arraylist

时间:2016-01-02 12:55:54

标签: android arraylist android-asynctask

目前我正在寻找一种方法将几个私有decalared 字符串值(fac_naam,fac_straat,fac_post等)传递给arraylist。 arraylist由方法 SetStandardGroups 创建,asynctask中的收集值应传递给 child_2 值。 现在,child_2值在新创建时始终为空。 有没有办法做到这一点?

先谢谢你。

拨打电话:

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

    // get the listview
    ExpandList = (ExpandableListView) rootView.findViewById(R.id.expListView);

    // execute AsyncTask
    new FetchWebsiteData().execute();

    // preparing list data
    ExpListItems = SetStandardGroups();

    ExpAdapter = new ExpandableListAdapter(this.getActivity(), ExpListItems);

    // setting list adapter
    ExpandList.setAdapter(ExpAdapter);
    return rootView;
}

方法SetStandardGroups:

private ArrayList<Group> SetStandardGroups() {

    String headers[] = {"Factuurgegevens", "Contactgegevens", "Betalingsgegevens",
            "MijnZiggo Mobiel account", "Wachtwoord"};

    String child_1[] = {"Naam", "Straat en huisnummer", "Postcode en woonplaats", "Telefoonummer",
            "E-mail", "Rekeningnummer", "Naam rekeninghouder", "Gebruikersnaam", "Wachtwoord"};

    String child_2[] = {fac_naam, fac_straat, fac_post, con_tel,
            con_email, betaal_reknr, betaal_houd, zig_gebruiker, zig_wacht};

    ArrayList<Group> list = new ArrayList<Group>();

    ArrayList<Child> ch_list; 
    }
    return list;
}

正在设置值的AsyncTask:

private class FetchWebsiteData extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... result) {
        InputStream is = null;
        try {
            is = MainActivity.mContext.getAssets().open("Mobiel.html");

            Document doc = Jsoup.parse(is, "UTF-8", "http://example.com/");
            // Get the html document title
            Elements els = doc.select("span[class=item-value pull-right]");
            // get the values and set them
            if (els.size() > 0) {
                fac_naam = els.get(0).text();
                fac_straat = els.get(1).text();
                fac_post = els.get(2).text();
                con_tel = els.get(3).text();
                con_email = els.get(4).text();
                betaal_reknr = els.get(5).text();
                betaal_houd = els.get(6).text();
                zig_gebruiker = els.get(7).text();
                zig_wacht = els.get(8).text();
            }
            else // do something

        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {

   }
}

1 个答案:

答案 0 :(得分:0)

您自己遇到了并发问题。当你这样做时:

// execute AsyncTask
new FetchWebsiteData().execute();

// preparing list data
ExpListItems = SetStandardGroups();

在调用SetStandardGroups()之前,AsyncTask可能尚未完成执行,因此变量仍然为null。您需要更改它,以便在AsyncTask完成后调用适配器代码。我做了一个新的方法,如:

private void setupAdapter() {

    // preparing list data
    ExpListItems = SetStandardGroups();

    ExpAdapter = new ExpandableListAdapter(this.getActivity(), ExpListItems);

    // setting list adapter
    ExpandList.setAdapter(ExpAdapter);
}

然后在AsyncTask的onPostExecute()方法中调用新方法:

@Override
protected void onPostExecute(Void result) {

    setupAdapter();
}