Android SimpleAdapter刷新

Android SimpleAdapter刷新,android,json,Android,Json,我在刷新屏幕上的数据时遇到问题,应用程序通过JSON获取数据一切正常,但现在我需要执行菜单中的刷新按钮。我根据一个例子构建了我的应用程序。据我所知,使用SimpleAdapter是个好主意,需要编写一个自定义适配器。你能帮我吗 ServiceHandler public class ServiceHandler { static String response = null; public final static int GET = 1; public final static int POS

我在刷新屏幕上的数据时遇到问题,应用程序通过JSON获取数据一切正常,但现在我需要执行菜单中的刷新按钮。我根据一个例子构建了我的应用程序。据我所知,使用SimpleAdapter是个好主意,需要编写一个自定义适配器。你能帮我吗

ServiceHandler

public class ServiceHandler {
static String response = null;
public final static int GET = 1;
public final static int POST = 2;

public ServiceHandler() {

}

/*
 * Making service call
 * @url - url to make request
 * @method - http request method
 * */
public String makeServiceCall(String url, int method) {
    return this.makeServiceCall(url, method, null);
}

/*
 * Making service call
 * @url - url to make request
 * @method - http request method
 * @params - http request params
 * */
public String makeServiceCall(String url, int method,
        List<NameValuePair> params) {
    try {
        // http client
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpEntity httpEntity = null;
        HttpResponse httpResponse = null;

        // Checking http request method type
        if (method == POST) {
            HttpPost httpPost = new HttpPost(url);
            // adding post params
            if (params != null) {
                httpPost.setEntity(new UrlEncodedFormEntity(params));
            }

            httpResponse = httpClient.execute(httpPost);

        } else if (method == GET) {
            // appending params to url
            if (params != null) {
                String paramString = URLEncodedUtils
                        .format(params, "utf-8");
                url += "?" + paramString;
            }
            HttpGet httpGet = new HttpGet(url);

            httpResponse = httpClient.execute(httpGet);

        }
        httpEntity = httpResponse.getEntity();
        response = EntityUtils.toString(httpEntity);

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

    return response;

}
}
公共类ServiceHandler{
静态字符串响应=null;
公共最终静态int GET=1;
公共最终静态int POST=2;
公共服务处理程序(){
}
/*
*拨打服务电话
*@url-发出请求的url
*@method-http请求方法
* */
公共字符串makeServiceCall(字符串url,int方法){
返回此.makeServiceCall(url,方法,null);
}
/*
*拨打服务电话
*@url-发出请求的url
*@method-http请求方法
*@params-http请求参数
* */
公共字符串makeServiceCall(字符串url,int方法,
列表参数){
试一试{
//http客户端
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpEntity HttpEntity=null;
HttpResponse HttpResponse=null;
//检查http请求方法类型
if(方法==POST){
HttpPost HttpPost=新的HttpPost(url);
//添加post参数
如果(参数!=null){
setEntity(新的UrlEncodedFormEntity(参数));
}
httpResponse=httpClient.execute(httpPost);
}else if(方法==GET){
//将参数附加到url
如果(参数!=null){
String paramString=URLEncodedUtils
.格式(参数“utf-8”);
url+=“?”+参数字符串;
}
HttpGet HttpGet=新的HttpGet(url);
httpResponse=httpClient.execute(httpGet);
}
httpEntity=httpResponse.getEntity();
response=EntityUtils.toString(httpEntity);
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
返回响应;
}
}
main活动

public class MainActivity extends ListActivity {

    private ProgressDialog pDialog;

    // URL to get contacts JSON
    private static String url = "******";

    // JSON Node names
    private static final String TAG_ZLECENIA = "zlecenia";
    private static final String TAG_ID = "id";
    private static final String TAG_CO = "co";
    private static final String TAG_KYDA = "kyda";
    private static final String TAG_ILE = "ile";
    private static final String TAG_LIM = "lim_czas";


    // zlecenia JSONArray
    JSONArray zlecenia = null;

    // Hashmap for ListView
    ArrayList<HashMap<String, String>> ZleceniaList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ZleceniaList = new ArrayList<HashMap<String, String>>();

        ListView lv = getListView();

        // Listview on item click listener
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // getting values from selected ListItem
                String co = ((TextView) view.findViewById(R.id.co))
                        .getText().toString();
                String kyda = ((TextView) view.findViewById(R.id.kyda))
                        .getText().toString();
            }
        });

        // Calling async task to get json
        new GetContacts().execute();
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    /**
     * Async task class to get json by making HTTP call
     * */
    private class GetContacts extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Showing progress dialog
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Please wait...");
            pDialog.setCancelable(false);
            pDialog.show();

        }

        @Override
        protected Void doInBackground(Void... arg0) {
            // Creating service handler class instance
            ServiceHandler sh = new ServiceHandler();

            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

            Log.d("Response: ", "> " + jsonStr);

            if (jsonStr != null) {
                try {
                    JSONObject jsonObj = new JSONObject(jsonStr);

                    // Getting JSON Array node
                    zlecenia = jsonObj.getJSONArray(TAG_ZLECENIA);

                    // looping through All Contacts
                    for (int i = 0; i < zlecenia.length(); i++) {
                        JSONObject c = zlecenia.getJSONObject(i);

                        String id = c.getString(TAG_ID);
                        String co = c.getString(TAG_CO);
                        String kyda = c.getString(TAG_KYDA);
                        String ile = c.getString(TAG_ILE);
                        String lim = c.getString(TAG_LIM);

                        // tmp hashmap for single contact
                        HashMap<String, String> zlecenia = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        zlecenia.put(TAG_ID, id);
                        zlecenia.put(TAG_CO, co);
                        zlecenia.put(TAG_KYDA, kyda);
                        zlecenia.put(TAG_ILE, ile);
                        zlecenia.put(TAG_LIM, lim);

                        // adding contact to contact list
                        ZleceniaList.add(zlecenia);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            // Dismiss the progress dialog
            if (pDialog.isShowing())
                pDialog.dismiss();
            /**
             * Updating parsed JSON data into ListView
             * */
            ListAdapter adapter = new SimpleAdapter(
                    MainActivity.this, ZleceniaList, R.layout.list_item,
                    new String[] { TAG_KYDA, TAG_CO, TAG_ILE, TAG_LIM,},
                    new int[] { R.id.kyda, R.id.co, R.id.ile, R.id.lim });

            setListAdapter(adapter);
        }

    }

}
public类MainActivity扩展了ListActivity{
私人对话;
//获取联系人JSON的URL
私有静态字符串url=“*******”;
//JSON节点名称
私有静态最终字符串标记_ZLECENIA=“ZLECENIA”;
私有静态最终字符串标记\u ID=“ID”;
私有静态最终字符串标记_CO=“CO”;
私有静态最终字符串标记_KYDA=“KYDA”;
私有静态最终字符串标记_ILE=“ILE”;
私有静态最终字符串标记\u LIM=“LIM\u czas”;
//兹勒塞尼亚
JSONArray zlecenia=null;
//ListView的Hashmap
ArrayList ZleceniaList;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ZleceniaList=newarraylist();
ListView lv=getListView();
//单击项目上的Listview侦听器
lv.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
//从选定的ListItem获取值
字符串co=((TextView)view.findViewById(R.id.co))
.getText().toString();
字符串kyda=((TextView)view.findViewById(R.id.kyda))
.getText().toString();
}
});
//调用异步任务以获取json
新建GetContacts().execute();
}
公共布尔onCreateOptions菜单(菜单){
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
/**
*异步任务类通过HTTP调用获取json
* */
私有类GetContacts扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
//显示进度对话框
pDialog=新建进度对话框(MainActivity.this);
setMessage(“请稍候…”);
pDialog.setCancelable(假);
pDialog.show();
}
@凌驾
受保护的Void doInBackground(Void…arg0){
//创建服务处理程序类实例
ServiceHandler sh=新的ServiceHandler();
//向url发出请求并获得响应
字符串jsonStr=sh.makeServiceCall(url,ServiceHandler.GET);
Log.d(“响应:”、“>”+jsonStr);
if(jsonStr!=null){
试一试{
JSONObject jsonObj=新的JSONObject(jsonStr);
//获取JSON数组节点
zlecenia=jsonObj.getJSONArray(TAG_zlecenia);
//通过所有触点循环
对于(int i=0;ivalue
zlecenia.put(标签ID,ID);
zlecenia.put(TAG_CO,CO);
zlecenia.put(TAG_KYDA,KYDA);
zlecenia.put(标签,标签);
zlecenia.put(TAG_LIM,LIM);
//将联系人添加到联系人列表
ZleceniaList.add(zlecenia);
}
}捕获(JSO)
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity" >
    <item
        android:id="@+id/action_refresh"
        android:showAsAction="ifRoom"
        android:title="Refresh"
        android:icon="@drawable/ic_refresh_white_36dp"

         />
</menu>