下面是我从我的 Web 服务填充 ListView 的代码。
- 如何从 cookie 中设置和获取此 ListView?(我的代码上有一个例子)
- 假设我有一个按钮可以刷新此列表,如何处理?
- 如果用户连接到互联网如何处理?
我基本上想知道如何管理从 WebService 填充的 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() {
}
}