从另一个片段返回后,ListView适配器拒绝填充片段中的列表视图

时间:2013-12-08 22:00:16

标签: android listview android-listview android-fragments

我的代码是可以访问的,但适配器不起作用。列表视图没有显示,片段仍为空。

我在OnResume重新填充了我的列表。

我的代码:

public class mylist extends ListFragment {

    public void ToastLoadShout(String msg) {
        Toast.makeText(getActivity(), msg.toString(), Toast.LENGTH_LONG).show();
    }
    private static View View;

    HttpClient client;
    HttpPost httppost;
    HttpGet httpget;
    JSONObject json;
    List<List<String>> items;
    List<item> markers = new ArrayList<item>();

    MobileArrayAdapter adapter;

    ListView list;
    ProgressBar listload;
    Button relist;

    Preferences pref;

    String datadata = "";

    String savedlat="0.0";
    String savedlon="0.0";

    boolean isLoaded=false;
    int index;
    int top;

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


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override 
    public void onResume(){
        super.onResume();
        // Recreate the adapter from the new feed
        adapter = new MobileArrayAdapter(getActivity(), markers);
        // Set the recreated adapter
        list.setAdapter(adapter); 
        ToastLoadShout("reached here");
        if (!(items==null))
            ToastLoadShout("there are items");
    }

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

        if (!isLoaded){
        try {
            pref = new Preferences(getActivity());
            list = (ListView) getView().findViewById(android.R.id.list);
            listload = (ProgressBar) getView().findViewById(R.id.listload);
            HashMap<String, String> loc = pref.getData();
            ToastLoadShout(loc.get(Preferences.LAT) + ","
                    + loc.get(Preferences.LON));
            if (loc.get(Preferences.LAT) != null && !loc.get(Preferences.LAT).equals("0.0"))
            {

                if (loc.get(Preferences.LAT) != savedlat && loc.get(Preferences.LON)!=savedlon){
                new Load().execute();
                savedlat=loc.get(Preferences.LAT);
                savedlon=loc.get(Preferences.LON);
                }
            }
            else
                ToastLoadShout("Get Location First.");

            relist = (Button) getView().findViewById(R.id.relist);
            relist.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    listload.setVisibility(View.INVISIBLE);
                    HashMap<String, String> loc = pref.getData();
                    ToastLoadShout(loc.get(Preferences.LAT) + ","
                            + loc.get(Preferences.LON));
                    if (loc.get(Preferences.LAT) != null && !loc.get(Preferences.LAT).equals("0.0")){
                        adapter.deleteList();
                        list.destroyDrawingCache();
                        new Load().execute();}
                    else
                        ToastLoadShout("Get Location First.");
                }});

            } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }
        }


        list.setOnScrollListener(new OnScrollListener() {
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, 
                int visibleItemCount, int totalItemCount) {
                //Check if the last view is visible
                index = list.getFirstVisiblePosition();
                View v = list.getChildAt(0);
                top = (v == null) ? 0 : v.getTop();
            }

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                // TODO Auto-generated method stub

            }
        });
    }


    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {

        // get selected items
        //String selectedValue = (String) getListAdapter().getItem(position);
        String selectedValue = markers.get(position).getTitle();
        Toast.makeText(getActivity(), selectedValue, Toast.LENGTH_SHORT).show();

    }


    class Load extends AsyncTask<String, Integer, Boolean> {
        @Override
        protected void onPreExecute() {
            listload.setVisibility(View.VISIBLE);
            isLoaded=true;
            ToastLoadShout("preExecute");
        }

        @Override
        protected Boolean doInBackground(String... params) {

            try {
                items = DownloadList();
                if (items != null)
                    return true;

            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
            return false;
        }

        @Override
        protected void onPostExecute(Boolean res) {
            // TODO Auto-generated method stub
            if (res) {
                ArrangePutMarkers();
                adapter=new MobileArrayAdapter(getActivity(), markers);
                list.setAdapter(adapter);
            } else {
                ToastLoadShout("Error");
                ToastLoadShout(datadata);
            }
            listload.setVisibility(View.INVISIBLE);
            ToastLoadShout("PostExecute");
        }
    }

    public void onBackPressed() {

    }

}

1 个答案:

答案 0 :(得分:1)

更改其内容后调用适配器的notifydatasetchanged方法。您的OnResume方法正在为列表视图分配一个空数据适配器。

您在哪里填充标记集合?

此外,您可以调用ListFragment类的getListView和setListAdapter方法,而不是使用findViewById查找列表。