2015-06-05 45 views
0

我有刷新屏幕上的数据的问题,应用程序通过JSON获取数据一切正常,但现在我需要在菜单中执行刷新按钮。根据一个例子我构建了我的应用程序。据我了解,使用SimpleAdapter并需要编写自定义适配器是个不错的主意。你能帮我吗?Android 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; 

} 
} 

MainActivity

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); 
     } 

    } 

} 

休息/菜单/ main.xml中

<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> 

回答

0

您必须覆盖onOptionsItemSelected()方法的并处理刷新菜单项单击操作。

+0

感谢您的回答,也许您知道一些示例或教程? – hilsofbeard

+0

@hilsofbeard,看看Android开发者教程将会非常有用。这里是如何使用ActionBar菜单: http://developer.android.com/training/basics/actionbar/adding-buttons.html – Seishin