//布局文件1
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.hzx.mylistview1.MainActivity">
<Button
android:id="@+id/btn_scan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="top"
android:text="扫描"/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/pname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:textSize="15sp"
android:layout_weight="1"
android:text="产品名称"/>
<TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:textSize="15sp"
android:layout_weight="1"
android:text="产品价格"/>
<TextView
android:id="@+id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:textSize="15sp"
android:layout_weight="1"
android:text="产品产地"/>
</LinearLayout>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
//布局文件2
<?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="match_parent">
<TextView
android:id="@+id/pname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:textSize="15sp"
android:layout_weight="1"
android:text="产品名称"/>
<TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:textSize="15sp"
android:layout_weight="1"
android:text="产品价格"/>
<TextView
android:id="@+id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:textSize="15sp"
android:layout_weight="1"
android:text="产品产地"/>
</LinearLayout>
//activity1
public class MyDataSource {
private List<Map<String,String>> listMaps;
public MyDataSource(){
listMaps = new ArrayList<Map<String,String>>();
}
public void setMaps(String name,String price,String address){
Map<String,String> map1 = new HashMap<String,String>();
map1.put("pname",name);
map1.put("price",price);
map1.put("address",address);
listMaps.add(map1);
}
public List<Map<String,String>>getMaps(){
/*
Map<String,String> map1 = new HashMap<String,String>();
map1.put("pname","西瓜");
map1.put("price","$2.30");
map1.put("address","海南");
Map<String,String> map2 = new HashMap<String,String>();
map2.put("pname","香蕉");
map2.put("price","$4.30");
map2.put("address","广西");
Map<String,String> map3 = new HashMap<String,String>();
map3.put("pname","南瓜");
map3.put("price","$1.30");
map3.put("address","河北");
listMaps.add(map1);
listMaps.add(map2);
listMaps.add(map3);
*/
return listMaps;
}
}
//activity2
public class MainActivity extends AppCompatActivity {
private ListView listView;
private SimpleAdapter adapter;
private Button btn_scan;
private List<Map<String, String>> data = null;
private MyDataSource My_Data_Source;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listview);
My_Data_Source = new MyDataSource();
data = My_Data_Source.getMaps();
adapter = new SimpleAdapter(MainActivity.this, data, R.layout.listview_layout,
new String[]{"pname", "price", "address"}, new int[]{R.id.pname, R.id.price, R.id.address});
listView.setAdapter(adapter);
btn_scan = (Button)findViewById(R.id.btn_scan);
btn_scan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
My_Data_Source.setMaps("苹果","1.1","广西");
adapter.notifyDataSetChanged();
}
});
}
}
效果
//每点击一下添加一条Item