菜品信息展示案例
xml代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="菜品信息展示"
android:textSize="43dp"
android:textColor="@color/black">
</TextView>
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/lv">
</ListView>
</LinearLayout>
caixml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView android:layout_width="80dp"
android:layout_height="80dp"
android:id="@+id/tupian"/>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/caiming"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/jiage"
android:textColor="#F00"/>
</LinearLayout>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/jianjie"/>
</LinearLayout>
</LinearLayout>
Java代码:
package com.example.dome01;
import android.os.Bundle;
import android.app.Activity;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends Activity {
private ListView lv;
private int [] tp = {R.drawable.qcczg,R.drawable.yxrs,R.drawable.ymjrw}; //图片
private String [] cm = {"芹菜炒猪肝","鱼香肉丝","油面筋肉丸"}; //菜名
private double [] jg = {77,56,98}; //价格
private String [] jj = {"芹菜炒猪肝真的好吃","鱼香肉丝yyds!","油面筋肉丸牛是真的牛"}; //简介
private List<Map<String, Object>> datas = new ArrayList<Map<String,Object>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView)findViewById(R.id.lv);
for(int i=0;i<=2;i++){
Map<String, Object> map = new HashMap<String, Object>();
map.put("tupian", tp[i]);
map.put("caiming", cm[i]);
map.put("jiage", jg[i]+"¥");
map.put("jianjie", jj[i]);
datas.add(map);
}
String [] biaoti = {"tupian","caiming","jiage","jianjie"}; //数据的标题
int [] weizhi = {R.id.tupian,R.id.caiming,R.id.jiage,R.id.jianjie}; //标题对应的数据放在魔板里面的位置
SimpleAdapter ada = new SimpleAdapter(this, datas, R.layout.cai, biaoti, weizhi);
lv.setAdapter(ada);
}
}
效果展示:
要求:1.学会布局
2.学会列表视图的使用