android xml 列表展示,Android中ListView实现展示列表数据
1、在activity_main.xml中添加一個ListView
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
android:layout_height="wrap_content"
android:id="@+id/lvList">
2、新建一個layout文件用來作為list的一行格式文件
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
tools:context="${relativePackage}.${activityClass}" >
android:id="@+id/tvId"
android:layout_width="30dip"
android:layout_height="wrap_content"
android:textSize="25sp"
android:gravity="left"
/>
android:id="@+id/tvName"
android:textSize="25sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
android:id="@+id/tvAge"
android:textSize="25sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
3、package com.zlz.androidxml;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import com.zlz.androidxml.domain.Person;
import com.zlz.androidxml.service.ParseService;
import com.zlz.androidxml.service.PullParseServiceImpl;
import com.zlz.androidxml.service.SaxParseServiceImpl;
public class MainActivity extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btnPull).setOnClickListener(this);
}
@Override
public void onClick(View v) {
List persons = new ArrayList();
for(int i =1;i<4;i++){
Person person = new Person();
person.id = i;
person.name = "lizhi"+i;
person.age = 12+i;
persons.add(person);
}
popListView(persons);
}
//將List放到一個grid里面展示
private void popListView(List persons) {
List> ls = new ArrayList>();
Map map = null;
//需要將對象封裝為一個map的集合
for (Person p : persons) {
map = new HashMap();
map.put("id", p.id);
map.put("name", p.name);
map.put("age", p.age);
ls.add(map);
}
//使用標(biāo)簽ListView存放
ListView lvList = (ListView) findViewById(R.id.lvList);
//將list中每個對象虛擬為一個Item,然后再存入Grid中的每一行,listitemlayout就相當(dāng)于一個item
ListAdapter adapter = new SimpleAdapter(this, ls, R.layout.listitemlayout,
new String[] {"id","name","age"}, new int[] {R.id.tvId,R.id.tvName,R.id.tvAge});
lvList.setAdapter(adapter);
}
}
將文件放在TableLayout中
1、在activity_main.xml中添加Table_layout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
tools:context="${relativePackage}.${activityClass}" >
android:id="@+id/btnPull"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Pull" />
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="0,1,2"
android:id="@+id/tlLayout"
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ID"
/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="NAME"
/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="AGE"
/>
2、在MainActivity中循環(huán)創(chuàng)建Row然后添加到Table上
package com.zlz.androidxml;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import com.zlz.androidxml.domain.Person;
public class MainActivity extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btnSax).setOnClickListener(this);
findViewById(R.id.btnDom).setOnClickListener(this);
findViewById(R.id.btnPull).setOnClickListener(this);
}
@Override
public void onClick(View v) {
List persons = new ArrayList();
for(int i =1;i<4;i++){
Person person = new Person();
person.id = i;
person.name = "lizhi"+i;
person.age = 12+i;
persons.add(person);
}
//獲取TableLayout
TableLayout tl = (TableLayout) findViewById(R.id.tlLayout);
int childrenCount = tl.getChildCount();
// 防止每次查詢重復(fù)添加,所以每次拼裝為table時,除了表頭,其他的全部干掉
for (int i = childrenCount - 1; i > 0; i--) {
View view = tl.getChildAt(i);
tl.removeView(view);
}
while (cursor.moveToNext()) {
TableRow row = new TableRow(this);
TextView idView = new TextView(this);
idView.setText(cursor.getString(cursor.getColumnIndex("id")));
row.addView(idView);
TextView nameView = new TextView(this);
nameView.setText(cursor.getString(cursor.getColumnIndex("name")));
row.addView(nameView);
TextView ageView = new TextView(this);
ageView.setText(cursor.getString(cursor.getColumnIndex("age")));
row.addView(ageView);
// 將每一行添加到table上
tl.addView(row);
}
}
}
總結(jié)
以上是生活随笔為你收集整理的android xml 列表展示,Android中ListView实现展示列表数据的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 集合迭代器_Java中的集合迭
- 下一篇: java 布尔逻辑运算符_Java运算符