use of com.v2ex.api.TopicList in project V2HOT by djyde.
the class TitleListActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_title_list);
final SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.pull);
final ArrayAdapter<Topic> topicsAdapter = new ArrayAdapter<Topic>(this, 0) {
private final LayoutInflater inflater = LayoutInflater.from(getContext());
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.topic_item, parent, false);
}
TextView title = (TextView) convertView.findViewById(R.id.title);
title.setText(getItem(position).title);
return convertView;
}
};
final ListView topicsView = (ListView) findViewById(R.id.topics);
topicsView.setAdapter(topicsAdapter);
topicsView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Topic topic = topicsAdapter.getItem(position);
Intent intent = new Intent(TitleListActivity.this, ContentActivity.class);
intent.putExtra("id", topic.id);
intent.putExtra("title", topic.title);
intent.putExtra("username", topic.member.username);
intent.putExtra("url", topic.url);
intent.putExtra("content", topic.content);
startActivity(intent);
}
});
//创建volley请求队列
final RequestQueue queue = Volley.newRequestQueue(this);
//根据API获取热议主题
final GsonRequest<TopicList> request = new GsonRequest<TopicList>(Request.Method.GET, "https://www.v2ex.com/api/topics/hot.json", TopicList.class, new Response.Listener<TopicList>() {
@Override
public void onResponse(TopicList response) {
topicsAdapter.clear();
topicsAdapter.addAll(response);
topicsAdapter.notifyDataSetChanged();
swipeRefreshLayout.setRefreshing(false);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(TitleListActivity.this, "请检查网络", Toast.LENGTH_LONG).show();
}
});
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
queue.add(request);
}
});
queue.add(request);
swipeRefreshLayout.setRefreshing(true);
}
Aggregations