日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

android xml 列表展示,Android中ListView实现展示列表数据

發(fā)布時(shí)間:2024/9/27 Android 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android xml 列表展示,Android中ListView实现展示列表数据 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1、在activity_main.xml中添加一個(gè)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、新建一個(gè)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放到一個(gè)grid里面展示

private void popListView(List persons) {

List> ls = new ArrayList>();

Map map = null;

//需要將對象封裝為一個(gè)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中每個(gè)對象虛擬為一個(gè)Item,然后再存入Grid中的每一行,listitemlayout就相當(dāng)于一個(gè)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時(shí),除了表頭,其他的全部干掉

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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。