空列表视图(列表片段)

时间:2014-04-21 09:31:12

标签: java android android-listview android-listfragment

当应用程序运行时,我看到空布局“无数据”。我能做什么? 尝试AsyncTask,但是做了很多工作。我不知道该怎么办。我是android新手。 可能是我用错了方法?试图夸大视图并不是一个好主意?

public class MyEmployeFragment extends ListFragment {


    MyTask task;
    static String employer_id;
    static String employer_name;
    static String employer_last_name;
    String str = null;
    ArrayList<spr_item> ret_data = null;
    // имена атрибутов для Map
    final String ATTRIBUTE_ID   = "p_id";
    final String ATTRIBUTE_NAME = "p_name";
    final String ATTRIBUTE_LAST_NAME = "p_last_name";
    View v;
    ListView listView;
    SimpleAdapter sAdapter;

    ListView lvSimple;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        task    = new MyTask();
        task.execute();

    }


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

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                                     Bundle savedInstanceState)  {

        View v = inflater.inflate(R.layout.my_employe, container, false);
        listView = (ListView) v.findViewById(android.R.id.list);

        // Updating the action bar title
        getActivity().getActionBar();

        // массивы данных
        // String[] userId      = {employer_id};
        // String[] userName    = {employer_name};
        // String[] userLatName = {employer_last_name};

        ret_data = new  ArrayList<spr_item>();
        //ret_data.add(new spr_item("11","22","33"));
        //ret_data.add(new spr_item("111","222","333"));
        //ret_data.add(new spr_item("1111","2222","3333"));

        // упаковываем данные в понятную для адаптера структуру
        ArrayList<Map<String, Object>> data = new ArrayList<Map<String, Object>>(
                ret_data.size());
        Map<String, Object> m;
        for (int i = 0; i < ret_data.size(); i++) {
            m = new HashMap<String, Object>();
            m.put(ATTRIBUTE_ID, ret_data.get(i).getId() );
            m.put(ATTRIBUTE_NAME,ret_data.get(i).getName() );
            m.put(ATTRIBUTE_LAST_NAME,ret_data.get(i).getLastName() );
            data.add(m);
        }

        // массив имен атрибутов, из которых будут читаться данные
        String[] from = { ATTRIBUTE_ID, ATTRIBUTE_NAME,ATTRIBUTE_LAST_NAME};
        // массив ID View-компонентов, в которые будут вставлять данные
        int[] to = { R.id.tw_employe_id ,R.id.tw_employe_name, R.id.tw_employe_last_name};

        // создаем адаптер
        SimpleAdapter sAdapter = new SimpleAdapter(getActivity(), data, R.layout.list_item_employee,
                from, to);

        // определяем список и присваиваем ему адаптер
        lvSimple = (ListView) v.findViewById(android.R.id.list);
        lvSimple.setAdapter(sAdapter);

        return v;

    }

    class MyTask extends AsyncTask<Void, Void, Void> {

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

        }

        @Override
        protected Void doInBackground(Void... params) {

                String s = "5ACACEC6-752B-4EFF-AA50-EEBE58A52113";
                // String user_guid = myPrefs.getString("guid", "");

                HttpActivity _http = new HttpActivity("192.168.10.11", "80");
                _http.set_addr_protocol("/WebSite/P/spr/spr.aspx/");
                _http.add_param("query", "spr_employee_get");
                // _http.add_param("p_guid", user_guid.toString().trim());
                _http.add_param("p_guid", s);
                _http.send();

                ret_data = _http.getArrayParamValue();

                //employer_name = _http.getArrayParamValue("p_name");
                //employer_id = _http.getArrayParamValue("p_id");
                //employer_last_name = _http.getArrayParamValue("p_last_name");

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);


        }
    }

}

1 个答案:

答案 0 :(得分:0)

那是因为当您实例化适配器时,您的数据是空的。您应该在AsyncTask的onPostExecute方法中设置适配器,或者调用sAdapter.notifyDataSetChanged,以便适配器将使用获得的数据更新视图