这是在MainActivity的显示方法
public class MainActivity extends AppCompatActivity {
private ListView listView;
private PersonService personService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
personService = new PersonService(this);
listView = (ListView) findViewById(R.id.listView);
show();
}
private void show() {
List<Person> personList = personService.getScrollDate(1, 20);
List<HashMap<String,Object>> data= new ArrayList<HashMap<String, Object>>();
HashMap<String,Object> item;
for(Person person:personList)
{
item = new HashMap<String,Object>();
item.put("id",person.getId());
item.put("name",person.getName());
item.put("phone", person.getPhone());
data.add(item);
}
//适配器用来实现数据与条目,条目与listView的绑定。
SimpleAdapter simpleAdapter = new SimpleAdapter(this,data,R.layout.item,new String[]{"id","name","phone"},new int[]{R.id.id,R.id.name,R.id.phone});
listView.setAdapter(simpleAdapter);
}
这是item.xml内容
<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:layout_width="150dip"
android:layout_height="wrap_content"
android:id="@+id/id"
/>
<TextView
android:layout_width="100dip"
android:layout_height="wrap_content"
android:id="@+id/name"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/phone"
/>
</LinearLayout>
这是activity_main.xml内容
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<TextView
android:layout_width="150dip"
android:layout_height="wrap_content"
android:text="@string/id"
/>
<TextView
android:layout_width="100dip"
android:layout_height="wrap_content"
android:text="@string/name"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/phone"
/>
</LinearLayout>
<ListView
android:background="#ff0000"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/listView"
/>
</LinearLayout>