//记住加判断网络的权限<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.xlistviewlib.XListView;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private SharedPreferences.Editor edit;
private List<Bean.DataBean> mList;
private XListView lv;
private SharedPreferences share;
int page=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (XListView)findViewById(R.id.listview);
mList = new ArrayList<>();
MyApdater myApdater = new MyApdater();
lv.setAdapter(myApdater);
share = getSharedPreferences("share", MODE_PRIVATE);
edit = share.edit();
dohttp(page);
huancun();
lv.setPullLoadEnable(true);
lv.setXListViewListener(new XListView.IXListViewListener() {
@Override
public void onRefresh() {
mList.clear();
dohttp(page);
}
@Override
public void onLoadMore() {
page++;
if(page>1) {
dohttp(page);
}
}
});
}
private void dohttp(int page) {
String url = "http://www.xieast.com/api/news/news.php?type=top&page="+page;
new HttpHelper().get(url).restult(new HttpHelper.Httplistnear() {
@Override
public void success(String data) {
//Toast.makeText(MainActivity.this,"sdf"+data,Toast.LENGTH_SHORT).show();
Gson gson = new Gson();
Bean bean = gson.fromJson(data, Bean.class);
List<Bean.DataBean> data1 = bean.getData();
edit.putString("data",data).commit();
mList.addAll(data1);
lv.stopRefresh();
lv.stopLoadMore();
}
});
}
private boolean huancun(){
ConnectivityManager connectivityManager = (ConnectivityManager)this.getApplicationContext().getSystemService(this.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
if(activeNetworkInfo!=null&&activeNetworkInfo.isAvailable()){
return true;
}else{
String data = share.getString("data", "");
Log.i("的地方大师傅", "huancun: "+data);
Toast.makeText(MainActivity.this,"sdf"+data,Toast.LENGTH_SHORT).show();
// Gson gson = new Gson();
// Bean bean = gson.fromJson(data, Bean.class);
// List<Bean.DataBean> data1 = bean.getData();
// mList.addAll(data1);
// lv.stopRefresh();
// lv.stopLoadMore();
return false;
}
}
private class MyApdater extends BaseAdapter {
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder;
if (view==null){
view = View.inflate(MainActivity.this,R.layout.listview_item,null);
holder = new ViewHolder();
holder.mImage = (ImageView) view.findViewById(R.id.image);
holder.mTitle = (TextView) view.findViewById(R.id.title);
view.setTag(holder);
}else{
holder = (ViewHolder) view.getTag();
}
holder.mTitle.setText(mList.get(i).getTitle());
Picasso.with(MainActivity.this).load(mList.get(i).getThumbnail_pic_s()).into(holder.mImage);
//ImageLoader.getInstance().displayImage(mList.get(i).getThumbnail_pic_s(),holder.mImage);
return view;
}
}
class ViewHolder{
ImageView mImage;
TextView mTitle;
}
}
ljj
最新推荐文章于 2021-07-26 20:32:15 发布