以下是我从网络服务中 输入ListView的代码。
- How can I set and get this ListView from cookies? (There`s an example on my code)
- Let`s say I have a button that will refresh this list, how to handle it?
- How to handle if the user is connected to internet or not?
我想知道如何管理一个来自网络服务的 ListView 。
public class EventsActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard);
RequestParams params = new RequestParams();
params.put("user_id", "1");
BloompItClient.get("get_events", params, new AsyncHttpResponseHandler() {
@Override
public void onStart() {
}
@Override
public void onSuccess(String response) {
JSONObject jsonResponse;
try {
jsonResponse = new JSONObject(response);
try {
String state = jsonResponse.getString("state");
if (state.equals("1")) {
ArrayList<HashMap<String, String>> eventsList = new ArrayList<HashMap<String, String>>();
JSONArray events = jsonResponse.getJSONArray("events");
for (int i = 0; i < events.length(); i++) {
JSONObject currentEvent = events.getJSONObject(i);
HashMap<String, String> eventMap = new HashMap<String, String>();
eventMap.put("college", currentEvent.getString("college_username"));
eventMap.put("author", currentEvent.getString("user_username"));
eventMap.put("title", currentEvent.getString("title"));
eventMap.put("date", currentEvent.getString("date"));
eventsList.add(eventMap);
}
ListView eventsListView = (ListView) findViewById(R.id.events);
EventsListAdapter eventsListAdapter = new EventsListAdapter(getApplicationContext(), eventsList);
// testing cookies
AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
PersistentCookieStore persistentCookieStore = new PersistentCookieStore(getApplicationContext());
asyncHttpClient.setCookieStore(persistentCookieStore);
BasicClientCookie baseClientCookie = new BasicClientCookie("cookiesare", "awesome");
baseClientCookie.setVersion(1);
baseClientCookie.setDomain("it.bloomp");
baseClientCookie.setPath("/");
persistentCookieStore.addCookie(baseClientCookie);
List<Cookie> cookieList = persistentCookieStore.getCookies();
for (int i = 0; i < cookieList.size(); i++) {
System.out.println(cookieList.toString());
}
// testing cookies end
eventsListView.setAdapter(eventsListAdapter);
} else {
}
} catch (JSONException e) {
e.printStackTrace();
}
} catch (JSONException e1) {
e1.printStackTrace();
}
}
@Override
public void onFailure(Throwable e) {
}
@Override
public void onFinish() {
}
});
}
public void getEvents() {
}
}