在异步响应之后更新片段中的列表

时间:2015-06-29 01:21:27

标签: android android-fragments

我有一个包含项目列表的片段。我试图根据异步api响应更新列表。我的条件逻辑工作正常,我的日志消息正常工作。一旦有几次列表确实更新但它不一致。

如何确保列表正确更新。

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    ViewGroup v = (ViewGroup) inflater.inflate(R.layout.deal_container,
            container, false);
    loadContent(v);

    return v;
}

/**
 * Load content for the activity .
*/
private void loadContent(View v) {
    list = (ListView) v.findViewById(R.id.listView1);
    info = new ArrayList<DataDeals>();

    info.add(new DataDeals(R.drawable.daily_deal_pic1,
            getString(R.string.blue_mountains),
            getString(R.string.australian), "150", "100", "45"));

    APICaller.makeAPICall("country-deals",
            "Default Message", Collections.<String, Object>emptyMap(),
            new Target.TargetCallback<String>() {

                @Override
                public void call(final String content) {
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (content.equals("us")) {

                                Log.d("US is calling!----", content);
                                info.add(new DataDeals(R.drawable.daily_deal_pic1,
                                        getString(R.string.blue_mountains),
                                        getString(R.string.liberty), "150", "100", "45"));

                            } else if (content.equals("uk")) {

                                Log.d("UK is calling----", content);
                                info.add(new DataDeals(R.drawable.london_eye,
                                        getString(R.string.blue_mountains),
                                        getString(R.string.londoneye), "150", "100", "1000"));


                            } else if (content.equals("aus")) {

                                Log.d("Australia is calling--", content);
                                info.add(new DataDeals(R.drawable.london_eye,
                                        getString(R.string.blue_mountains),
                                        getString(R.string.australian), "100", "75", "145"));

                            }
                        }
                    });
                }
            });


    adapter1 = new DealItemAdapter(getActivity(), info);
    list.setAdapter(adapter1);
    list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            launchActivity(position, info);
        }
    });

private void launchActivity(final int position, List<DataDeals> info) {
    startActivity(new Intent(getActivity().getApplicationContext(),
            BuyScreen.class)
            .putExtra("title", info.get(position).getTxt1()));
}

0 个答案:

没有答案