80,359
社区成员
发帖
与我相关
我的任务
分享
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import com.jikexueyuan.news.adapter.NewsAdapter;
import com.jikexueyuan.news.model.News;
import com.jikexueyuan.news.utils.HttpUtils;
public class MainActivity extends Activity implements OnItemClickListener{
private ListView lvNews;
private NewsAdapter adapter;
private List<News> newsList;
public static final String GET_NEWS_URL = "http://www.zjhaining.com/api/get_recent_posts?post_type=news&page=1&count=20&include=id,title,excerpt,date,comment_count,custom_fields";
private Handler getNewsHandler = new Handler(){
public void handleMessage(android.os.Message msg) {
String jsonData = (String) msg.obj;
System.out.println(jsonData);
try {
JSONArray jsonArray = new JSONArray(jsonData);
for (int i=0;i<jsonArray.length();i++){
JSONObject object = jsonArray.getJSONObject(i);
String id = object.getString("id");
String title = object.getString("title");
String excerpt = object.getString("excerpt");
String date = object.getString("date");
String custom_fields = object.getString("custom_fields");
String thumb_value = object.getString("thumb_value");
System.out.println("title = " +title);
System.out.println("thumb_value = " +thumb_value);
newsList.add(new News(id, title, excerpt, date,custom_fields, thumb_value ));
}
adapter.notifyDataSetChanged();
} catch (Exception e) {
e.printStackTrace();
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lvNews = (ListView) findViewById(R.id.lvNews);
newsList = new ArrayList<News>();
adapter = new NewsAdapter(this, newsList);
lvNews.setAdapter(adapter);
lvNews.setOnItemClickListener(this);
HttpUtils.getNewsJSON(GET_NEWS_URL, getNewsHandler);
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
News news = newsList.get(position);
Intent intent = new Intent(this, BrowseNewsActivity.class);
intent.putExtra("Excerpt", news.getExcerpt());
startActivity(intent);
}
}
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.jikexueyuan.news.R;
import com.jikexueyuan.news.model.News;
import com.jikexueyuan.news.utils.HttpUtils;
public class NewsAdapter extends BaseAdapter {
private Context context;
private List<News> newsList;
public NewsAdapter(Context context, List<News> newsList){
this.context = context;
this.newsList = newsList;
}
@Override
public int getCount() {
return newsList.size();
}
@Override
public News getItem(int position) {
return newsList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null){
convertView = LayoutInflater.from(context).inflate(R.layout.news_item, null);
}
TextView Title = (TextView) convertView.findViewById(R.id.Title);
TextView excerpt = (TextView) convertView.findViewById(R.id.excerpt);
TextView date = (TextView) convertView.findViewById(R.id.date);
ImageView thumb_value = (ImageView) convertView.findViewById(R.id.thumb_value);
News news = newsList.get(position);
Title.setText(news.getTitle());
excerpt.setText(news.getExcerpt());
date.setText(news.getDate());
String custom_fields = news.getCustom_fields();
HttpUtils.setPicBitmap(thumb_value,null);
return convertView;
}
}
public class News {
private String title;
private String excerpt;
private String date;
private String id;
private String thumb_value;
private String custom_fields;
public News(String title, String excerpt, String date, String id, String custom_fields,String thumb_value ){
setTitle(title);
setExcerpt(excerpt);
setDate(date);
setId(id);
setCustom_fields(custom_fields);
setThumb_value(thumb_value);}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getExcerpt() {
return excerpt;
}
public void setExcerpt(String excerpt) {
this.excerpt = excerpt;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getThumb_value() {
return thumb_value;
}
public void setThumb_value(String thumb_value) {
this.thumb_value = thumb_value;
}
public String getCustom_fields() {
return custom_fields;
}
public void setCustom_fields(String custom_fields) {
this.custom_fields = custom_fields;
}
}