Listview Adapter作业
Listview and Adapter作业
第一个XML部分代码
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listitem">
</ListView>
第二个XML部分代码
<LinearLayout
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:id="@+id/name" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/age" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/address" />
</LinearLayout>
然后就是JAVA的代码了
public class MainActivity extends AppCompatActivity {
private String[] name={"内马尔","梅西","C罗","格里兹曼","苏亚雷斯"};
private int [] age={25,30,31,26,30};
private String[] adress={"巴西","阿根廷","葡萄牙","法国","乌拉圭"};
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<Map<String, Object>> listems = new ArrayList<>();
for (int i = 0; i < name.length; i++) {
Map<String, Object> listem = new HashMap<>();
listem.put("name", "姓名:"+name[i]);
listem.put("age", "年龄:"+age[i]);
listem.put("adress", "地址:"+adress[i]);
listems.add(listem);
}
SimpleAdapter simplead = new SimpleAdapter(this, listems,R.layout.activity_lists2, new String[] { "name", "age", "adress" },
new int[] {R.id.name,R.id.age,R.id.address});
lv=(ListView)findViewById(R.id.listitem);
lv.setAdapter(simplead);
}
}
浙公网安备 33010602011771号