Main页面
package com.example.yu.xieyu20181105;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioGroup;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends FragmentActivity {
private ViewPager viewPager;
private List<Fragment> list;
private RadioGroup group;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager=findViewById(R.id.viewpager);
//向list里添加数据
list=new ArrayList<>();
list.add(new FragmentOne());
list.add(new FragmentTwo());
list.add(new FragmentThree());
group=findViewById(R.id.group);
//viewpager添加适配器
viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int i) {
return list.get(i);
}
@Override
public int getCount() {
return list.size();
}
});
//group单击按钮切换页面
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.radio01:viewPager.setCurrentItem(0);break;
case R.id.radio02:viewPager.setCurrentItem(1);break;
case R.id.radio03:viewPager.setCurrentItem(2);break;
}
}
});
viewPager.setCurrentItem(0);
group.check(R.id.radio01);
}
}
Bean类
package com.example.yu.xieyu20181105;
import java.util.List;
public class ContentBean {
public String info;
public int status;
public List<Date> data;
public ContentBean(String info, int status, List<Date> data) {
this.info = info;
this.status = status;
this.data = data;
}
public String getInfo() {
return info;
}
public int getStatus() {
return status;
}
public List<Date> getData() {
return data;
}
public static class Date{
public String news_title;
public String news_summary;
public Date(String news_title, String news_summary) {
this.news_title = news_title;
this.news_summary = news_summary;
}
public String getNews_title() {
return news_title;
}
public String getNews_summary() {
return news_summary;
}
}
}
list适配器
package com.example.yu.xieyu20181105;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class ContentAdapter extends BaseAdapter {
private List<ContentBean.Date> list;
private Context context;
public ContentAdapter(Context context) {
this.context = context;
list=new ArrayList<>();
}
public void setList(List<ContentBean.Date> list) {
this.list = list;
notifyDataSetChanged();
}
@Override
public int getCount() {
return list.size();
}
@Override
public ContentBean.Date getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
convertView=LayoutInflater.from(context).inflate(R.layout.item,parent,false);
holder=new ViewHolder(convertView);
}else{
holder= (ViewHolder) convertView.getTag();
}
holder.bind(getItem(position));
return convertView;
}
class ViewHolder{
TextView title;
TextView summary;
public ViewHolder(View convertView) {
title=convertView.findViewById(R.id.title);
summary=convertView.findViewById(R.id.summary);
convertView.setTag(this);
}
public void bind(ContentBean.Date item) {
title.setText(item.getNews_title());
summary.setText(item.getNews_summary());
}
}
}
FragmentOne
package com.example.yu.xieyu20181105;
import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
public class FragmentOne extends Fragment {
private NewsDao dao;
private ListView listView;
private ContentAdapter adapter;
private List<ContentBean.Date> dates;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.one,container,false);
dao=new NewsDao(getContext());
listView=view.findViewById(R.id.list_item);
adapter = new ContentAdapter(getContext());
listView.setAdapter(adapter);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
requestDate();
//长按条目删除
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
dao.del(dates.get(position).getNews_title());
dates.remove(position);
adapter.notifyDataSetChanged();
return true;
}
});
}
private String strurl="http://api.expoon.com/AppNews/getNewsList/type/1/p/1";
@SuppressLint("StaticFieldLeak")
private void requestDate(){
new AsyncTask<String, Void, List<ContentBean.Date>>() {
@Override
protected List<ContentBean.Date> doInBackground(String... strings) {
try {//异步请求数据
URL url = new URL(strurl);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setConnectTimeout(5000);
urlConnection.setReadTimeout(5000);
int responseCode = urlConnection.getResponseCode();
if(responseCode==200){
String result=streamToString(urlConnection.getInputStream());
//gson解析读出来的数据
ContentBean bean = new Gson().fromJson(result, ContentBean.class);
List<ContentBean.Date> data = bean.getData();
//判断数据库是否有数据
List<ContentBean.Date> select = dao.select();
if(select.size()==0){
//没有数据就添加数据
for (int i=0;i<data.size();i++){
dao.add(data.get(i));
}
}
//查询数据库里的数据
dates = dao.select();
return dates;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override//展示数据库里的内容
protected void onPostExecute(List<ContentBean.Date> dates) {
adapter.setList(dates);
}
}.execute(strurl,"");
}
private String streamToString(InputStream inputStream) throws IOException {
//读取数据
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder builder=new StringBuilder();
for (String tmp=bufferedReader.readLine();tmp!=null;tmp=bufferedReader.readLine()){
builder.append(tmp);
}
return builder.toString();
}
}
创建数据库
package com.example.yu.xieyu20181105;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class Sqlhelper extends SQLiteOpenHelper {
public Sqlhelper(Context context) {
//创建数据库
super(context, "news.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
//创建表
db.execSQL("create table new_table(id integer primary key autoincrement," +
"title text," +
"summary text)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
Dao层
package com.example.yu.xieyu20181105;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;
public class NewsDao {
private Sqlhelper sqlhelper;
private SQLiteDatabase database;
public NewsDao(Context context) {
sqlhelper=new Sqlhelper(context);
database=sqlhelper.getReadableDatabase();
}
public List<ContentBean.Date> select() {
List<ContentBean.Date> list=new ArrayList<>();
Cursor query = database.query("new_table", null, null, null, null, null, null);
while(query.moveToNext()){
String title = query.getString(query.getColumnIndex("title"));
String summary = query.getString(query.getColumnIndex("summary"));
list.add(new ContentBean.Date(title,summary));
}
return list;
}
public void add(ContentBean.Date date){
ContentValues values=new ContentValues();
values.put("title",date.getNews_title());
values.put("summary",date.getNews_summary());
database.insert("new_table",null,values);
}
public void del(String news_title) {
database.delete("new_table","title=?",new String[]{news_title});
}
}