ListView未在适配器加载上刷新

时间:2012-06-11 08:16:53

标签: android android-listview listadapter

我有一个标签主机,可将后续文件加载到其中一个标签

    package com.api.view;
    import java.io.IOException;
    import java.security.NoSuchAlgorithmException;
    import java.util.ArrayList;
    import java.util.HashMap;

    import javax.xml.parsers.ParserConfigurationException;

    import org.xml.sax.SAXException;

    import com.api.api.api;
    import com.api.app.R;
    import com.api.communication.reports;
    import com.api.sync.data.reportsList;
    import com.api.tools.serverCommunication;
    import com.api.tools.urlbuilder;

    import android.app.Activity;
    import android.app.ProgressDialog;
    import android.content.Context;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.BaseAdapter;
    import android.widget.ListView;
    import android.widget.TextView;

    public class EquipmentTabHistory extends Activity{
        ListView lv;
        ListViewAdapter adapter;
        ArrayList<HashMap<String, String>> menuItems;
        ProgressDialog pDialog;
        static final String KEY_ITEM = "item"; // parent node
        static final String KEY_ID = "id";
        static final String KEY_NAME = "name";
        private class ListViewAdapter extends BaseAdapter {

            private Activity activity;
            private ArrayList<HashMap<String, String>> data;
            private LayoutInflater inflater=null;
            public ListViewAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
                activity = a;
                data=d;
                inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            }
            public int getCount() {
                return data.size();
            }
            public Object getItem(int position) {
                return position;
            }
            public long getItemId(int position) {
                return position;
            }
            public View getView(int position, View convertView, ViewGroup parent) {
                View vi=convertView;
                if(convertView==null)
                    vi = inflater.inflate(R.layout.list_item, null);
                TextView name = (TextView)vi.findViewById(R.id.name);
                HashMap<String, String> item = new HashMap<String, String>();
                item = data.get(position);
                //Setting all values in listview
                name.setText(item.get("name"));
                return vi;
            }
        }
        private class loadMoreListView extends AsyncTask<Void, Void, Void> {
            protected Void doInBackground(Void... unused) {
                try {
                    reportsList al;
                    int eid = Integer.parseInt(getIntent().getStringExtra("eid"));
                    String[][] surl = new String[1][2];
                    surl[0][0] = "eid";
                    surl[0][1] = ""+eid;
                    String url;
                    url = urlbuilder.buildurl("getEquipmentHistory", surl);
                    api.debug("com.api.view.EquipmentTabHistory","URL: "+url,"info");
                    String pull = serverCommunication.pull(url);
                    reports reports;
                    reports = new reports(pull);
                    for(int i=0;i<reports.getList().size();i++){
                        al = reports.getList().get(i);
                        HashMap<String, String> map = new HashMap<String, String>();
                        map.put(KEY_ID,reports.getList().get(i).did);
                        long timestamp = Integer.parseInt(al.date) * 1000;  // msec  
                        java.util.Date d = new java.util.Date(timestamp);
                        map.put(KEY_NAME,al.ReportId + " - "+al.Typ + " - " + al.description + "("+d.getMonth()+". "+cT(d.getDay())+" "+(1900 + d.getYear())+" - "+d.getHours()+":"+cT(d.getMinutes())+")");
                        menuItems.add(map);
                    }
                } catch (Exception e) {
                    api.debug("com.api.view.EquipmentTabHistory","NSAE: "+e.getMessage(),"error");
                }
                return (null);
            }
            private String cT(int time){
                String r = "";
                r += (time>9)?time:(time>0)?"0"+time:time;
                return r;
            }
        }
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.api_tab_history);
            lv = (ListView) findViewById(R.id.listViewEquipment);
            menuItems = new ArrayList<HashMap<String, String>>();
            adapter = new ListViewAdapter(this, menuItems);
            lv.setAdapter(adapter);
            new loadMoreListView().execute();
            lv.setOnItemClickListener(new OnItemClickListener(){
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
                    api.debug("com.api.view.EquipmentTabHistory","Clicked: "+arg2+"-"+arg3,"warn");
                    api.debug("com.api.view.EquipmentTabHistory","Clicked: "+((TextView)findViewById(R.id.name)).getText().toString(),"warn");
                    api.debug("com.api.view.EquipmentTabHistory","Selected: "+menuItems.get(arg2).get(KEY_ID),"warn");
                    String[][] surl = new String[2][2];
                    surl[0][0] = "did";
                    surl[0][1] = menuItems.get(arg2).get(KEY_ID);
                    surl[1][0] = "eid";
                    surl[1][1] = getIntent().getStringExtra("eid");
                    String docurl;
                    try {
                        docurl = urlbuilder.buildurlNoXml("getDocument",surl,true);
                        api.debug(com.api.view.EquipmentTabHistory.class.toString(),"URL: "+docurl,"info");
                        System.out.println(docurl);
                        Intent browser = new Intent(Intent.ACTION_VIEW , Uri.parse( docurl ) );
                        startActivity(browser);
                    } catch (NoSuchAlgorithmException e) {
                        api.debug("com.api.view.EquipmentTabHistory","No Such Algorithm "+e.getMessage(),"error");
                    }
                }

            });
        }
    }

页面加载没有任何例外。 在日志中(通过api.debug),我可以看到文件已加载,并且还可以解析它。 但我必须切换选项卡才能看到更改。 一旦异步方法停止加载,我应该如何修改它刷新列表视图的代码?

1 个答案:

答案 0 :(得分:0)

您必须覆盖onPostExecute()内的另一个方法AsyncTask,如下所示:

@Override
protected Void onPostExecute(Void... unused) {
    adpater.notifyDatasetChanged();  // will tell the adapter to refresh itself
}