public class MainActivity extends AppCompatActivity {
private ListView lv;
private RequestQueue requestQueue;
private MyAdapter ad;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv= (ListView) findViewById(R.id.listview);
getVolly();
}
public void getVolly() {
requestQueue= Volley.newRequestQueue(this);
StringRequest s=new StringRequest(Request.Method.GET, "http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1", new Response.Listener<String>() {
@Override
public void onResponse(String s) {
FoodBean fb= JSONObject.parseObject(s,FoodBean.class);
ad=new MyAdapter(getApplicationContext(),fb);
lv.setAdapter(ad);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
}
});
requestQueue.add(s);
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public class MyAdapter extends BaseAdapter {
private FoodBean foodBean;
private Context context;
public MyAdapter(Context context,FoodBean foodBean){
this.foodBean=foodBean;
this.context=context;
}
@Override
public int getCount() {
if (foodBean.getData()!=null){
return foodBean.getData().size();
}
return 0;
}
@Override
public Object getItem(int position) {
if (foodBean.getData()!=null){
return foodBean.getData().get(position);
}
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder vh=null;
if (convertView==null){
vh=new ViewHolder();
convertView= LayoutInflater.from(context).inflate(R.layout.list_item,null);
vh.iv= (ImageView) convertView.findViewById(R.id.imageView);
vh.tv= (TextView) convertView.findViewById(R.id.textView);
vh.tv1= (TextView) convertView.findViewById(R.id.textView2);
convertView.setTag(vh);
}else{
vh= (ViewHolder) convertView.getTag();
}
vh.tv.setText(foodBean.getData().get(position).getTitle());
vh.tv1.setText("数量:"+foodBean.getData().get(position).getCollect_num());
Glide.with(context).load(foodBean.getData().get(position).getPic()).into(vh.iv);
return convertView;
}
class ViewHolder{
ImageView iv;
TextView tv;
TextView tv1;
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@mipmap/ic_launcher"
android:layout_marginLeft="28dp"
android:layout_marginStart="28dp"
android:id="@+id/imageView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="17dp" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="29dp"
android:layout_marginStart="29dp"
android:id="@+id/textView"
android:layout_alignTop="@+id/imageView"
android:layout_toRightOf="@+id/imageView"
android:layout_toEndOf="@+id/imageView"
android:layout_marginTop="19dp" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView2"
android:layout_marginRight="24dp"
android:layout_marginEnd="24dp"
android:layout_alignBaseline="@+id/textView"
android:layout_alignBottom="@+id/textView"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />