Listview and Adapte
一、XML代码
1、<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listitem">
</ListView>
2、<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={"科比","詹姆斯","库里","张伯伦","乔丹"};
private int [] age={38,33,29,63,54};
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);
}
}