数据库中的数据就不再贴上 了,可以自行写。
eclipse中的类和studio和类一样。eclipse中的关键代码为:
case “game_list”:
List list=null;
try {
list=new Game_list_dao().game_list();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Gson gson2=new Gson();
String result2=gson2.toJson(list);
printWriter.write(result2);
printWriter.close();
System.out.println(“SendData_Game_list.doGet()”+result2);
break;
default:
break;
}
}
然后studio中的XListView事先配好。
activity中的代码为:
package com.example.fgc20.activity;
import android.content.Context;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import com.example.fgc20.R;
import com.example.fgc20.zjn.bean.Game;
import com.example.fgc20.zjn.util.App;
import com.example.fgc20.zjn.util.XListView;
import com.facebook.drawee.view.SimpleDraweeView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class Game_stand_line_Activity extends AppCompatActivity implements XListView.IXListViewListener {
private XListView listView;
//适配器的类要跟数据的类是一样的
private gameAdapter gameadapter;
//数据集合
private List game;
private Handler handler;
Toolbar toolbar;
Button load_game;
//请求路径地址
String url;
Gson gson;
RequestParams params;
//游戏总量,还有每次显示的条数
int startIndex=0;
int pageCount=5;
int gameCount=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_stand_line_);
initData();
toolbar.setTitle("单机游戏");
setSupportActionBar(toolbar);
}
private void initData() {
handler=new Handler();//使用之前要初始化
listView= (XListView) findViewById(R.id.game_stand_list);
listView.setPullLoadEnable(true);//下拉加载
listView.setPullRefreshEnable(true);//上拉刷新
game=new ArrayList<Game>();
//gson和URL使用之前初始化
gson=new Gson();
url= App.getUrl();
params=new RequestParams(url);
getConnectWeb();
//导航栏
toolbar= (Toolbar) findViewById(R.id.toolbar);
//设置监听
listView.setXListViewListener((XListView.IXListViewListener) this);
}
//绑定toolbar中的各个item
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.toolbar_1,menu);
return true;
}
//下拉刷新界面
@Override
public void onRefresh() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
//清空item里面的数据
game.clear();
//重新获取网络数据
getConnectWeb();
//由于适配器加载和item重新载入数据已经在适配器中完成,所以不用写在刷新这里
//获取完数据要停止刷新和加载
onLoad();
}
},2000);
}
//由于是一次性加载所有数据,上拉加载就没有数据显示了。
@Override
public void onLoadMore() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
gameadapter.notifyDataSetChanged();
onLoad();
}
},2000);
}
//刷新和加载后都要停止,同时把时间设置为刚刚
private void onLoad(){
listView.stopRefresh();
listView.stopLoadMore();
listView.setRefreshTime(“刚刚”);
}
//获取网络数据
public Object getConnectWeb() {
params.addQueryStringParameter(“flag”,”game_list”);
x.http().get(params, new Callback.CommonCallback() {
@Override
public void onSuccess(String result) {
Type type=new TypeToken<List<Game>>(){}.getType();
集合加载全部数据
game.addAll((Collection<? extends Game>) gson.fromJson(result,type));
if (gameadapter==null){
gameadapter=new gameAdapter(Game_stand_line_Activity.this,game);
listView.setAdapter(gameadapter);
}else {
gameadapter.notifyDataSetChanged();
}
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
});
return game;
}
//游戏列表适配器
class gameAdapter extends BaseAdapter{
Context context;
List game;
LayoutInflater inflater;
public gameAdapter(Context context,List game){
this.context=context;
this.game=game;
this.inflater=LayoutInflater.from(context);
}
@Override
public int getCount() {
return game.size();
}
@Override
public Object getItem(int i) {
return game.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
class ViewHolder{
SimpleDraweeView img_game;
TextView name_game;
TextView size_game;
TextView pnum_game;
TextView detail_game;
}
ViewHolder viewHolder;
@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
Log.i(“wccc”,”getView”+i);
if (view==null){
view= LayoutInflater.from(context).inflate(R.layout.game_stand_list,null);
viewHolder=new ViewHolder();
viewHolder.img_game= (SimpleDraweeView) view.findViewById(R.id.gameimg);
viewHolder.name_game= (TextView) view.findViewById(R.id.gamename);
viewHolder.size_game= (TextView) view.findViewById(R.id.gamesize);
viewHolder.pnum_game= (TextView) view.findViewById(R.id.gamepnum);
viewHolder.detail_game= (TextView) view.findViewById(R.id.gamedetails);
view.setTag(viewHolder);
}else {
viewHolder= (ViewHolder) view.getTag();
}
final Game ga=game.get(i);
viewHolder.img_game.setImageURI(ga.getGame_img());
viewHolder.name_game.setText(ga.getGame_name());
viewHolder .size_game.setText(ga.getGame_size());
viewHolder.pnum_game.setText(ga.getGame_pnum()+”“);
viewHolder.detail_game.setText(ga.getGame_pres());
return view;
}
}
}
其中包含了适配器。
布局文件有