Android中ListView实现展示列表数据

1、在activity_main.xml中添加一个ListView

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context="${relativePackage}.${activityClass}" >

	<ListView android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:id="@+id/lvList">
	</ListView>
</LinearLayout>

2、新建一个layout文件用来作为list的一行格式文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    tools:context="${relativePackage}.${activityClass}" >
<!-- dip指的是像素,sp指的是字体大小 -->
    <TextView
        android:id="@+id/tvId"
        android:layout_width="30dip"
        android:layout_height="wrap_content"
        android:textSize="25sp"
        android:gravity="left"
         />

    <TextView
        android:id="@+id/tvName"
        android:textSize="25sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        />

    <TextView
        android:id="@+id/tvAge"
        android:textSize="25sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
         />

</LinearLayout>
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<Person> persons = new ArrayList<Person>();
		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<Person> persons) {
		List<Map<String, Object>> ls = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = null;
		//需要将对象封装为一个map的集合
		for (Person p : persons) {
			map = new HashMap<String, Object>();
			map.put("id", p.id);
			map.put("name", p.name);
			map.put("age", p.age);
			ls.add(map);
		}
		//使用标签ListView存放
		ListView lvList = (ListView) findViewById(R.id.lvList);
		//将list中每个对象虚拟为一个Item,然后再存入Grid中的每一行,listitemlayout就相当于一个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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context="${relativePackage}.${activityClass}" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         android:orientation="horizontal"
        tools:context="${relativePackage}.${activityClass}" >


        <Button
            android:id="@+id/btnPull"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Pull" />
    </LinearLayout>
<!--strechColumns 保证列平分-->
   <TableLayout
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:stretchColumns="0,1,2"
		android:id="@+id/tlLayout"
		>
		<TableRow>
			<TextView
				android:layout_width="fill_parent"
				android:layout_height="wrap_content"
				android:text="ID"
				/>
			<TextView
				android:layout_width="fill_parent"
				android:layout_height="wrap_content"
				android:text="NAME"
				/>
			<TextView
				android:layout_width="fill_parent"
				android:layout_height="wrap_content"
				android:text="AGE"
				/>
		</TableRow>
	</TableLayout>
</LinearLayout>

2、在MainActivity中循环创建Row然后添加到Table上

</pre><pre name="code" class="java">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<Person> persons = new ArrayList<Person>();
		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();
// 防止每次查询重复添加,所以每次拼装为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);
}

		
	}

}



  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值