Android+PHP+MySQL实现新闻列表(一)

来源:互联网 发布:域名过户需要多长时间 编辑:程序博客网 时间:2024/06/04 00:34

在Android开发中,很多时候需要与数据库连接,读取插入修改数据库内容,那么数据库就非常重要了,在众多的数据库中,MySQL数据库算是简单好用的一个了,通常形式是Android+PHP+MySQL这样的组合

接下来以显示新闻列表为例,细说Android+PHP+MySQL实现新闻列表

我用的是Xampp,里面数据库都是图形化的操作,首先显示在里面新建一个数据库test,在test下建立数据表news,表有五个字段,分别是title、desc、time、content_url、img_url,分别存放题目、简介、发布时间、内容链接、图片链接等信息


第一部分是数据库连接读取部分

首先编辑conn.php文件,这个文件里写的是连接数据库最基本的信息

<?php$con = mysql_connect("localhost","root","");//设置字符集mysql_query("SET NAMES 'utf8'");mysql_query("SET CHARACTER SET utf8");mysql_query("SET CHARACTER_SET_RESULT=utf8");if (!$con) {die(mysql_error());}mysql_select_db("test",$con);?>

这样就连接上了名为test的数据库了

一般来说数据库都是通过JSON数据格式传递信息的,所以接下来要写接收JSON数据的getJson.php

<?phprequire 'conn.php';//引入conn.php文件$result = mysql_query("select * from news");//从test数据库的news数据表查询$n=0;while ($row = mysql_fetch_array($result)) {//依次取出数据库中的信息$arr[$n++] = array( 'title' =>$row['title'],'desc' =>$row['desc'],'time' =>$row['time'],'content_url' =>$row['content_url'],'img_url' =>$row['img_url'] );}echo json_encode($arr);//转变为JSON格式输出?>
至此PHP这部分就写完了,主要是通过PHP连接数据库,并从数据库中读取信息并以JSON的形式输出供我们使用


接下来Android部分

首先是页面布局

因为要实现新闻列表显示,所以在content_main.xml文件中添加ListView

 <ListView        android:id="@+id/lvNews"        android:padding="20dp"        android:layout_width="wrap_content"        android:layout_height="wrap_content">    </ListView>

再新建一个news_item.xml用于设置新闻列表的样式

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <ImageView        android:id="@+id/img"        android:layout_width="40dp"        android:layout_height="40dp"        android:src="@mipmap/ic_launcher"/>    <TextView        android:id="@+id/title"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_toRightOf="@id/img"        android:text="Title"        android:textSize="20sp"/>    <TextView        android:id="@+id/desc"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_toRightOf="@id/img"        android:layout_below="@id/title"        android:text="Desc"        android:textSize="12sp"/>    <TextView        android:id="@+id/time"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:text="Time"        android:textSize="10sp"/></RelativeLayout>


这是布局的样式,注意右上角是时间显示哦

在MainActivity中要findViewById找到 lvNews,想要对ListView外观行为进行定制,就需要吧ListView作为AdapterView来使用,通过Adapter控制,所以listview需要绑定适配器Adapter,Adapter主要是用于把数据映射到ListView上,说白了就是数据往哪填要靠adapter决定。ListView常用的adapter有ArrayAdapter、BaseAdapter、SimpleAdapter,其中ArrayAdapter功能有限,每个列表项只能是TextView,而扩展BaseAdapter可以获得Adapter最大的控制权,所以可以新建一个NewsAdapter extends Baseadapter

package com.news;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 java.util.ArrayList;import java.util.List;/** * Created by Administrator on 2016-08-25. */public class NewsAdapter extends BaseAdapter {    private Context context;    private List<News> newsList = new ArrayList<>();    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 desc = (TextView) convertView.findViewById(R.id.desc);        TextView time = (TextView) convertView.findViewById(R.id.time);        ImageView img = (ImageView) convertView.findViewById(R.id.img);        News news = newsList.get(position);        title.setText(news.getTitle());        desc.setText(news.getDesc());        time.setText(news.getTime());        String img_url = news.getImg_url();        HttpUtils.setBitmap(img,img_url);        return convertView;    }}
新建一个List存放news的数据,实现BaseAdapter中的四个方法,在getView这写adapter的返回视图,getView()方法决定第position处的列表项组件

在convertView为空,即没有加载布局时,加载我们已经是定义好的news_item.xml布局,加载后设置对应的属性并返回视图,注意,图片是通过网络请求访问图片连接来获得图片,所以需要网络请求那部分,暂时先放放再说这部分。

还需要定义一个News类来set和get到news的各个属性

package com.news;/** * Created by Administrator on 2016-08-25. */public class News {    private String title;    private String desc;    private String time;    private String content_url;    private String img_url;    public News(String title,String desc,String time,String content_url,String img_url){        setTitle(title);        setDesc(desc);        setTime(time);        setContent_url(content_url);        setImg_url(img_url);    }    public String getTitle(){        return title;    }    public String getDesc(){        return desc;    }    public String getTime(){        return time;    }    public String getContent_url(){        return content_url;    }    public String getImg_url(){        return img_url;    }    public void setTitle(String title){        this.title = title;    }    public void setDesc(String desc){        this.desc = desc;    }    public void setTime(String time){        this.time = time;    }public void setContent_url(String content_url){        this.content_url = content_url;    }public void setImg_url(String img_url) {        this.img_url = img_url;    }}




0 0
原创粉丝点击