package liyuanqi.bwie.com.pulltorrfresh;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
public class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public Boolean wangluo(Context context){
ConnectivityManager systemService = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = systemService.getActiveNetworkInfo();
if(systemServicenull){
return false;
}
if(activeNetworkInfonull||!activeNetworkInfo.isAvailable()){
return false;
}
return true;
}
}
package liyuanqi.bwie.com.pulltorrfresh;
import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import java.net.HttpURLConnection;
import java.util.List;
public class MainActivity extends BaseActivity {
PullToRefreshListView pulllist;
Myadapter myadapter;
boolean jian=false;
int page=1;
Dao dao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pulllist = findViewById(R.id.pulllist);
dao = new Dao(this);
myadapter = new Myadapter(MainActivity.this);
pulllist.setMode(PullToRefreshListView.Mode.BOTH);
pulllist.setAdapter(myadapter);
httphuoqu(page);
pulllist.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {
@Override
public void onPullDownToRefresh(PullToRefreshBase refreshView) {
page=1;
jian=false;
httphuoqu(page);
}
@Override
public void onPullUpToRefresh(PullToRefreshBase refreshView) {
page++;
jian=true;
httphuoqu(page);
}
});
}
@SuppressLint("StaticFieldLeak")
private void httphuoqu(int page) {
String wz="http://172.17.8.100/movieApi/movie/v1/findHotMovieList?count=3&page="+page;
new AsyncTask>() {
@Override
protected List doInBackground(String... strings) {
return HttpConnection.httpdate(strings[0]);
}
@Override
protected void onPostExecute(List resultBeans) {
super.onPostExecute(resultBeans);
if (wangluo(MainActivity.this)){
if (resultBeans==null){
Toast.makeText(MainActivity.this,"数据请求失败",Toast.LENGTH_SHORT).show();
return;
}else {
for (int i=0;i selectdata = dao.selectdata(R.mipmap.ic_launcher);
MyadapterTow myadapterTow = new MyadapterTow(MainActivity.this);
pulllist.setAdapter(myadapterTow);
myadapterTow.addlistdata(selectdata);
pulllist.onRefreshComplete();
}
if (jian){
Toast.makeText(MainActivity.this,"刷新成功",Toast.LENGTH_SHORT).show();
}
pulllist.onRefreshComplete();
}
}.execute(wz);
}
//判断
private void httpadapter(List resultBeans) {
if (page==1){
myadapter.listdata(resultBeans);
}else {
myadapter.addlistdata(resultBeans);
}
}
/* public void pagezhengjia(){
page++;
}*/
}
package liyuanqi.bwie.com.pulltorrfresh;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.ArrayList;
import java.util.List;
class Myadapter extends BaseAdapter{
private Context context;
List
public Myadapter(Context context) {
this.context = context;
list=new ArrayList<>();
}
public void listdata(List resultBeans) {
this.list.clear();
this.list.addAll(resultBeans);
notifyDataSetChanged();
}
public void addlistdata(List resultBeans) {
if (resultBeans!=null){
this.list.addAll(resultBeans);
notifyDataSetChanged();
}
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object 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) {
ViewHodler hodler;
if (convertView==null){
hodler=new ViewHodler();
convertView=View.inflate(context,R.layout.shuju,null);
hodler.title=convertView.findViewById(R.id.tit);
hodler.img=convertView.findViewById(R.id.img);
convertView.setTag(hodler);
}else {
hodler= (ViewHodler) convertView.getTag();
}
hodler.title.setText(list.get(position).getName());
ImageLoader.getInstance().displayImage(list.get(position).getImageUrl(),hodler.img);
return convertView;
}
class ViewHodler{
TextView title;
ImageView img;
}
}
package liyuanqi.bwie.com.pulltorrfresh;
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.URL;
import java.util.List;
public class HttpConnection {
static JsonBean jsonBean;
static JsonBean jsonBean2;
//数据
public static List
try {
URL url = new URL(wang);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setReadTimeout(5000);
urlConnection.setConnectTimeout(5000);
int code = urlConnection.getResponseCode();
if (code==200){
String streud=httpString(urlConnection.getInputStream());
jsonBean = new Gson().fromJson(streud, JsonBean.class);
}
return jsonBean==null?null:jsonBean.getResult();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private static String httpString(InputStream is) {
StringBuilder builder=new StringBuilder();
BufferedReader reader=new BufferedReader(new InputStreamReader(is));
try {
for (String temp=reader.readLine();temp!=null;temp=reader.readLine()){
builder.append(temp);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return builder.toString();
}
}
package liyuanqi.bwie.com.pulltorrfresh;
import android.app.Application;
import android.content.Context;
import android.graphics.Bitmap;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
ImageLoaderConfiguration configuration=new ImageLoaderConfiguration.Builder(this).build();
ImageLoader.getInstance().init(configuration);
}
public static DisplayImageOptions getdisplayImageOptions(Context context){
DisplayImageOptions displayImageOptions=new DisplayImageOptions.Builder()
.cacheOnDisk(true)
.cacheInMemory(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.delayBeforeLoading(R.mipmap.ic_launcher)
.build();
return displayImageOptions;
}
}
package liyuanqi.bwie.com.pulltorrfresh;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
public class Dao {
SQLiteDatabase database;
List list;
public Dao(Context context) {
Mysqlitehelper mysqlitehelper = new Mysqlitehelper(context);
database = mysqlitehelper.getReadableDatabase();
}
public void adddata(String name, int ic_launcher) {
database.execSQL("insert into user(name,img) values(?,?)",new Object[]{name,ic_launcher});
}
public List selectdata(int ic_launcher) {
list=new ArrayList<>();
Cursor cursor = database.rawQuery("select * from user", null);
Log.e("lyq",cursor+"");
while (cursor.moveToNext()){
String name = cursor.getString(cursor.getColumnIndex("name"));
int img = cursor.getInt(cursor.getColumnIndex("img"));
Prond prond = new Prond();
prond.setName(name);
prond.setImageUrl(img);
list.add(prond);
}
Log.e("lyq",list+"");
return list;
}
}
package liyuanqi.bwie.com.pulltorrfresh;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
class Mysqlitehelper extends SQLiteOpenHelper{
public Mysqlitehelper(Context context) {
super(context, "User.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table user(id Integer primary key autoincrement ,name text,img Integer)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
package liyuanqi.bwie.com.pulltorrfresh;
public class Prond {
String name;
int imageUrl;
public Prond(String name, int imageUrl) {
this.name = name;
this.imageUrl = imageUrl;
}
public Prond() {
}
public String getName() {
return name;
}
public int getImageUrl() {
return imageUrl;
}
public void setName(String name) {
this.name = name;
}
public void setImageUrl(int imageUrl) {
this.imageUrl = imageUrl;
}
@Override
public String toString() {
return "Prond{" +
"name='" + name + '\'' +
", imageUrl=" + imageUrl +
'}';
}
}
package liyuanqi.bwie.com.pulltorrfresh;
import android.content.Context;
import android.os.AsyncTask;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.ArrayList;
import java.util.List;
class MyadapterTow extends BaseAdapter{
private Context context;
List list;
public MyadapterTow(Context context) {
this.context = context;
list=new ArrayList<>();
}
public void addlistdata(List resultBeans) {
if (resultBeans!=null){
this.list.addAll(resultBeans);
notifyDataSetChanged();
}
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object 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) {
ViewHodler hodler;
if (convertView==null){
hodler=new ViewHodler();
convertView=View.inflate(context,R.layout.shuju,null);
hodler.title=convertView.findViewById(R.id.tit);
hodler.img=convertView.findViewById(R.id.img);
convertView.setTag(hodler);
}else {
hodler= (ViewHodler) convertView.getTag();
}
hodler.title.setText(list.get(position).getName());
hodler.img.setBackgroundResource(list.get(position).imageUrl);
return convertView;
}
class ViewHodler{
TextView title;
ImageView img;
}
}