比较综合的一个案例,同样的要导library包以及gson和picasso,在我们需要实现从网络获取数据时,要在manifests文件中加权限
<uses-permission android:name="android.permission.INTERNET"/>
先是MainActivity
public class MainActivity extends AppCompatActivity {
String url="http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=";
int index=1;
private PullToRefreshListView lv;
List<Map<String,String>> list;
MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(PullToRefreshListView)findViewById(R.id.lv);
list=new ArrayList<>();
adapter=new MyAdapter(this,list);
lv.setAdapter(adapter);
getData();
lv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
@Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) {
index=1;
list.clear();
getData();
}
});
lv.setOnLastItemVisibleListener(new PullToRefreshBase.OnLastItemVisibleListener() {
@Override
public void onLastItemVisible() {
index++;
getData();
}
});
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
list.remove(position);
adapter.notifyDataSetChanged();
return true;
}
});
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
HashMap<String,String> map=(HashMap<String, String>) list.get(position-1);
Food.DataBean bean=new Food.DataBean();
bean.setCollect_num(map.get("collnum"));
bean.setFood_str(map.get("str"));
bean.setId(map.get("id"));
bean.setNum(Integer.valueOf(map.get("num")));
bean.setPic(map.get("img"));
bean.setTitle(map.get("title"));
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
intent.putExtra("bean",bean);
startActivity(intent);
}
});
}
public void getData(){
new MyAsyncTask(new MyAsyncTask.CallBack() {
@Override
public void sendResult(String json) {
Gson gson=new Gson();
Food food = gson.fromJson(json, Food.class);
for (int i=0;i<food.getData().size();i++){
Map<String,String> map=new HashMap<String, String>();
map.put("img",food.getData().get(i).getPic());
map.put("title",food.getData().get(i).getTitle());
map.put("id",food.getData().get(i).getId());
map.put("num",String.valueOf(food.getData().get(i).getNum()));
map.put("collnum",food.getData().get(i).getCollect_num());
map.put("str",food.getData().get(i).getFood_str());
list.add(map);
}
adapter.notifyDataSetChanged();
lv.onRefreshComplete();
}
}).execute(url+index);
}
}
适配器
public class MyAdapter extends BaseAdapter {
List<Map<String,String>> list;
Context context;
public MyAdapter(Context context,List<Map<String,String>> list){
this.list=list;
this.context=context;
}
@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) {
final ViewHolder vh;
if(convertView==null){
vh=new ViewHolder();
convertView = View.inflate(context, R.layout.list_item, null);
final ImageView iv=(ImageView) convertView.findViewById(R.id.iv);
TextView tv=(TextView)convertView.findViewById(R.id.tv);
vh.vh_iv=iv;
vh.vh_tv=tv;
convertView.setTag(vh);
}else {
vh=(ViewHolder) convertView.getTag();
}
vh.vh_tv.setText(list.get(position).get("title"));
Picasso.with(context).load(list.get(position).get("img")).transform(new CropCircleTransformation()).into(vh.vh_iv);
return convertView;
}
class ViewHolder{
ImageView vh_iv;
TextView vh_tv;
}
}
实体类很简单,alt+ins
public class Food
{
/**
* ret : 1
* data : [{"id":"8289","title":"油焖大虾","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/9/8289.jpg","collect_num":"1639","food_str":"大虾 葱 生姜 植物油 料酒","num":1639},{"id":"2127","title":"四川回锅肉","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2127.jpg","collect_num":"1578","food_str":"猪肉 青蒜 青椒 红椒 姜片","num":1578},{"id":"30630","title":"超简单芒果布丁","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/31/30630.jpg","collect_num":"1507","food_str":"QQ糖 牛奶 芒果","num":1507},{"id":"9073","title":"家常红烧鱼","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/10/9073.jpg","collect_num":"1411","food_str":"鲜鱼 姜 葱 蒜 花椒","num":1411},{"id":"10097","title":"家常煎豆腐","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10097.jpg","collect_num":"1396","food_str":"豆腐 新鲜红椒 青椒 葱花 油","num":1396},{"id":"10509","title":"水煮肉片","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10509.jpg","collect_num":"1329","food_str":"瘦猪肉 生菜 豆瓣酱 干辣椒 花椒","num":1329},{"id":"46968","title":"红糖苹果银耳汤","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/47/46968.jpg","collect_num":"1238","food_str":"银耳 苹果 红糖","num":1238},{"id":"10191","title":"麻婆豆腐","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10191.jpg","collect_num":"1205","food_str":"豆腐 肉末 生抽 白糖 芝麻油","num":1205},{"id":"2372","title":"皮蛋瘦肉粥","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2372.jpg","collect_num":"1136","food_str":"大米 皮蛋 猪肉 油条 香葱","num":1136},{"id":"2166","title":"蚂蚁上树","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2166.jpg","collect_num":"1130","food_str":"红薯粉 肉 姜 蒜 花椒","num":1130},{"id":"2262","title":"糖醋肉","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2262.jpg","collect_num":"1064","food_str":"猪肉 红椒 黄椒 洋葱 蛋清","num":1064},{"id":"9971","title":"鱼香豆腐","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/10/9971.jpg","collect_num":"996","food_str":"豆腐 木耳 胡萝卜 香葱 番茄酱","num":996},{"id":"10172","title":"干煸四季豆","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10172.jpg","collect_num":"985","food_str":"四季豆 干辣椒 蒜头 酱油 糖","num":985},{"id":"2685","title":"胡萝卜肉末蒸蛋","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2685.jpg","collect_num":"902","food_str":"胡萝卜 肉 蛋 生抽 盐","num":902},{"id":"9972","title":"虎皮青椒","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/10/9972.jpg","collect_num":"883","food_str":"青辣椒 大蒜 香醋 白糖 生抽","num":883},{"id":"10437","title":"叉烧排骨","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10437.jpg","collect_num":"791","food_str":"排骨 李锦记叉烧酱 植物油 清水 油菜","num":791},{"id":"2892","title":"\u201c五行\u201d彩蔬汤","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2892.jpg","collect_num":"751","food_str":"黑木耳 玉米 牛蒡 胡萝卜 西兰花","num":751},{"id":"2348","title":"麻辣肉丝面","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2348.jpg","collect_num":"748","food_str":"面条 肉丝 淀粉 酱油 辣椒","num":748},{"id":"10044","title":"土豆炖翅根","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10044.jpg","collect_num":"745","food_str":"土豆 翅根 葱 姜 料酒","num":745},{"id":"33783","title":"美人豆浆","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/34/33783.jpg","collect_num":"744","food_str":"黄豆 红豆 绿豆 黑豆 黑米","num":744}]
*/
private int ret;
private List<DataBean> data;
public int getRet() {
return ret;
}
public void setRet(int ret) {
this.ret = ret;
}
public List<DataBean> getData() {
return data;
}
public void setData(List<DataBean> data) {
this.data = data;
}
public static class DataBean implements Serializable{
/**
* id : 8289
* title : 油焖大虾
* pic : http://www.qubaobei.com/ios/cf/uploadfile/132/9/8289.jpg
* collect_num : 1639
* food_str : 大虾 葱 生姜 植物油 料酒
* num : 1639
*/
private String id;
private String title;
private String pic;
private String collect_num;
private String food_str;
private int num;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic;
}
public String getCollect_num() {
return collect_num;
}
public void setCollect_num(String collect_num) {
this.collect_num = collect_num;
}
public String getFood_str() {
return food_str;
}
public void setFood_str(String food_str) {
this.food_str = food_str;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
}
异步任务类,主要任务是从网络获取数据
public class MyAsyncTask extends AsyncTask<String,Void,String> {
//Context context;
CallBack callBack;
public MyAsyncTask(CallBack callBack){
//this.context=context;
this.callBack=callBack;
}
@Override
protected String doInBackground(String... params) {
try {
URL url=new URL(params[0]);
HttpURLConnection huc=(HttpURLConnection) url.openConnection();
huc.setConnectTimeout(3000);
huc.connect();
if(huc.getResponseCode()==200){
InputStream inputStream = huc.getInputStream();
byte[] b=new byte[1024];
int len=0;
StringBuilder sb=new StringBuilder();
while ((len=inputStream.read(b))!=-1){
sb.append(new String(b,0,len));
}
return sb.toString();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
callBack.sendResult(s);
}
interface CallBack{
void sendResult(String json);
}
}
通过picasso获取网络数据并处理
public class Main2Activity extends AppCompatActivity {
private ImageView iv;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
iv=(ImageView)findViewById(R.id.iv);
tv=(TextView)findViewById(R.id.tv);
Intent intent = getIntent();
Food.DataBean bean=(Food.DataBean) intent.getSerializableExtra("bean");
String title = bean.getTitle();
String pic = bean.getPic();
tv.setText(title);
Picasso.with(this).load(pic).into(iv);
}
}
MainActivity布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
Main2Activity布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.dazonghe.Main2Activity">
<ImageView
android:id="@+id/iv"
android:layout_width="100dp"
android:layout_height="100dp" />
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
list_item布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iv"
android:layout_width="80dp"
android:layout_height="80dp" />
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:gravity="center_vertical"/>
</LinearLayout>