main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:<a href="http://lib.youkuaiyun.com/base/15" class="replace_word" title="undefined" target="_blank" style="color:#df3434; font-weight:bold;">android</a>="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"//注意一定要wrap_content
android:layout_height="wrap_content"//注意一定要wrap_content
android:orientation="horizontal" >
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="ID" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="NAME" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="AGE" />
</LinearLayout>
<ListView
android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
</ListView>
</LinearLayout>
item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/id1"
android:layout_width="100dp"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/name"
android:layout_width="100dp"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/age"
android:layout_width="100dp"
android:layout_height="wrap_content"
/>
</LinearLayout>
MainActivity.java
package org.xiazdong.db;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.xiazdong.db.domain.Person;
import org.xiazdong.db.service.DBService;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends Activity {
private ListView listView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView)this.findViewById(R.id.listview);
DBService service = new DBService(this);
List<Person> persons = service.pageQuery(0, 10);
List<HashMap<String,Object>> data = new ArrayList<HashMap<String,Object>>();
for(Person person:persons){
HashMap<String,Object>map = new HashMap<String,Object>();
map.put("id", person.getId());
map.put("name", person.getName());
map.put("age", person.getAge());
data.add(map);
}
System.out.println(data);
SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item, new String[]{"id","name","age"}, new int[]{R.id.id1,R.id.name,R.id.age});
//data表示显示的数据,一个Map为一行,List<Map>表示多行
//R.layout.item表示一个item的布局
//new String[]{"id","name","age"}表示将key="id"的值映射到R.id.id1上
listView.setAdapter(adapter);
}
}
本文详细介绍了Android应用开发中使用XML文件进行界面布局,并通过代码实现数据的展示,包括使用ListView组件和自定义适配器的实现过程。
1346

被折叠的 条评论
为什么被折叠?



