JAVA代码:
public class MainActivity extends Activity {
private ListView lv;
private MyasnyTast myasnyTast;//异步任务
private HttpURLConnection connection;//网络请求方式
private BitmapUtils bitmap;
private ArrayList<Data_list>list=new ArrayList<Data_list>( );
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView) findViewById(R.id.lv);
//实例化MyasnyTast类task只能被执行一次,否则多次调用时将会出现异常;
myasnyTast=new MyasnyTast();
myasnyTast.execute("http://api.sina.cn/sinago/list.json?channel=hdpic_story&adid=4ad30dabe134695c3d7c3a65977d7e72&from=6042095012&chwm=12050_0001&imei=867064013906290&uid=802909da86d9f5fc&p=1");
//listview 监听跳转
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
System.out.println(list.toString());
//传递网络请求数据
Intent it=new Intent(MainActivity.this, SencondActivity.class);
it.putExtra("list", (Serializable)list);
it.putExtra("getposition", position);
startActivity(it);
}
});
}
//异步任务AsyncTask定义了三种泛型类型 Params,Progress和Result。
class MyasnyTast extends AsyncTask<String, String, String>{
// 得到的结果处理操作
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
Gson gson=new Gson();
Person p=gson.fromJson(result , Person.class);
list=p.getData().getList();
//适配器
Myadapter myadapter=new Myadapter();
lv.setAdapter(myadapter);
}
//耗时操作 后台执行,比较耗时的操作都可以放在这里
protected String doInBackground(String... params) {
// 定义一个buff
StringBuffer buffer=new StringBuffer();
try {
URL url=new URL(params[0]);
//得到连接
connection=(HttpURLConnection) url.openConnection();
connection.setReadTimeout(10*1000);
//定义输出流,得到数据
if(connection.getResponseCode()==200){
InputStream is=connection.getInputStream();
//读取数据
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(is));
//定义字符串接受信息
String str="";
while ( (str=bufferedReader.readLine())!=null) {
buffer.append(str);
}
System.out.println(buffer.toString());
//关闭流
is.close();
bufferedReader.close();
//关闭网络请求
connection.disconnect();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return buffer.toString();
}
}
class Myadapter extends BaseAdapter{
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHoder h=null;
if(convertView==null){
h=new ViewHoder();
convertView=LayoutInflater.from(MainActivity.this).inflate(R.layout.data_items, null);
h.tv1=(TextView) convertView.findViewById(R.id.textView1);
h.tv2=(TextView) convertView.findViewById(R.id.textView2);
h.image =(ImageView) convertView.findViewById(R.id.imageView1);
convertView.setTag(h);
}else{
h=(ViewHoder) convertView.getTag();
}
h.tv1.setText("id:"+list.get(position).getId());
h.tv2.setText("标题"+list.get(position).getTitle());
BitmapUtils bitmap=new BitmapUtils(MainActivity.this);
bitmap.display(h.image, list.get(position).getPic());
return convertView;
}
class ViewHoder{
TextView tv1,tv2;
ImageView image;
}
}
}
2,创建数据库,添加到数据库中
public class Myopenheper extends SQLiteOpenHelper{
public Myopenheper(Context context, String name, CursorFactory factory,
int version) {
super(context, "1405ab", null, 1);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
//创建数据库
db.execSQL("create table news (_id integer primary key autoincrement ,id varchar(20)," +
"title varchar(29),pic varchar(20))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
3,第二个页面
public class SencondActivity extends Activity{
private TextView tv1,tv2;
private ImageView image;
private int getposition;
private Button button ,button2;
private Myopenheper myhelper;
private SQLiteDatabase db;
private ArrayList<Data_list>sen_list=new ArrayList<Data_list>( );
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sencond_items);
tv2=(TextView) findViewById(R.id.textView2);
tv1=(TextView) findViewById(R.id.textView1);
image=(ImageView) findViewById(R.id.imageView1);
button = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
//信息列表展示数据库信息
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent it3=new Intent(SencondActivity.this, ThreeActivity.class);
startActivity(it3);
}
});
//收藏监听
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String id=sen_list.get(getposition).getId();
String title=sen_list.get(getposition).getTitle();
String pic=sen_list.get(getposition).getPic();
//向数据库中添加数据
db.execSQL("insert into news values(null,?,?,?)", new Object[]{id,title,pic});
}
});
Intent it=getIntent();
sen_list=(ArrayList<Data_list>) it.getSerializableExtra("list");
getposition=it.getIntExtra("position", 0);
//创建数据库表
myhelper=new Myopenheper(SencondActivity.this, "1405ab", null, 1);
db=myhelper.getReadableDatabase();
//判断控件数据值大于5个字符以 ......代替
if(sen_list.get(getposition).getTitle().length()>5){
String str=sen_list.get(getposition).getTitle().substring(0,5);
String str2=sen_list.get(getposition).getId().substring(0,5);
tv2.setText(str+"......");
tv1.setText(str2);
}
//加载网络图片
BitmapUtils bitmapUtils=new BitmapUtils(SencondActivity.this);
bitmapUtils.display(image, sen_list.get(getposition).getPic());
}
}
4.数据库查询,展示信息列表
//1,从数据中查询收藏的数据,并用listview展示
public class ThreeActivity extends Activity{
private ListView lv2;
Myopenheper myhelper;
SQLiteDatabase db;
Data_list da ;
BitmapUtils bitmapUtils;
private ArrayList<Data_list>three_list=new ArrayList<Data_list>( );
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_three);
lv2=(ListView) findViewById(R.id.lv2);
MythreeAdapter adapter=new MythreeAdapter();
lv2.setAdapter(adapter);
myhelper=new Myopenheper(ThreeActivity.this, "1405ab", null, 1);
db=myhelper.getReadableDatabase();
//查询数据库
Cursor cursor=db.rawQuery("select * from news", null);
while (cursor.moveToNext()) {
String id=cursor.getString(cursor.getColumnIndex("id"));
String title=cursor.getString(cursor.getColumnIndex("title"));
String pic=cursor.getString(cursor.getColumnIndex("pic"));
da=new Data_list(id, title, pic);
three_list.add(da);
}
}
class MythreeAdapter extends BaseAdapter{
@Override
public int getCount() {
// TODO Auto-generated method stub
return three_list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return three_list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHodler h=null;
if(convertView==null){
h=new ViewHodler();
convertView=LayoutInflater.from(ThreeActivity.this).inflate(R.layout.three_items,null);
h.three_tv1=(TextView) convertView.findViewById(R.id.textView1);
h.three_tv2=(TextView) convertView.findViewById(R.id.textView2);
h.image=(ImageView) convertView.findViewById(R.id.imageView1);
convertView.setTag(h);
}else{
h=(ViewHodler) convertView.getTag();
}
h.three_tv1.setText(three_list.get(position).getId());
h.three_tv2.setText(three_list.get(position).getTitle());
BitmapUtils bitmapUtils=new BitmapUtils(ThreeActivity.this);
bitmapUtils.display(h.image, three_list.get(position).getPic());
return convertView;
}
class ViewHodler{
TextView three_tv1,three_tv2;
ImageView image;
}
}
}