展会信息港展会大全

android ListView用法
来源:互联网   发布日期:2015-11-26 13:57:54   浏览:2755次  

导读:print?<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.andro...

print?<?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="vertical" >

<LinearLayout

android:id="@+id/listLinearlayout"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="vertical" >

<ListView

android:id="@id/android:list"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

>

</ListView>

</LinearLayout>

</LinearLayout>

<?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="vertical" >

<LinearLayout

android:id="@+id/listLinearlayout"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="vertical" >

<ListView

android:id="@id/android:list"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

>

</ListView>

</LinearLayout>

</LinearLayout>

user.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/user_name"

android:layout_width="160dip"

android:layout_height="wrap_content"

android:paddingLeft="2dip"

android:paddingTop="2dip"

android:textSize="10pt"/>

<TextView

android:id="@+id/user_ip"

android:layout_width="160dip"

android:layout_height="wrap_content"

android:paddingRight="2dip"

android:paddingTop="2dip"

android:textSize="10pt"/>

</LinearLayout>

<?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/user_name"

android:layout_width="160dip"

android:layout_height="wrap_content"

android:paddingLeft="2dip"

android:paddingTop="2dip"

android:textSize="10pt"/>

<TextView

android:id="@+id/user_ip"

android:layout_width="160dip"

android:layout_height="wrap_content"

android:paddingRight="2dip"

android:paddingTop="2dip"

android:textSize="10pt"/>

</LinearLayout>

ListViewMain.java:

package com.example.wenandroid;

import java.util.ArrayList;

import java.util.HashMap;

import android.app.ListActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.ListView;

import android.widget.SimpleAdapter;

public class ListViewMain extends ListActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.listview);

ArrayList<HashMap<String,String>> list=new ArrayList<HashMap<String,String>>();

HashMap<String,String> map1=new HashMap<String,String>();

HashMap<String,String> map2=new HashMap<String,String>();

map1.put("user_name", "张三");

map1.put("user_ip", "192.168.0.110");

map2.put("user_name", "李四");

map2.put("user_ip", "192.168.0.111");

list.add(map1);

list.add(map2);

SimpleAdapter listAdapter=new SimpleAdapter(this, list, R.layout.user, new String[]{"user_name","user_ip"}, new int[]{R.id.user_name,R.id.user_ip});

setListAdapter(listAdapter);

}

protected void onListItemClick(ListView l,View v,int position,long id){

super.onListItemClick(l, v, position, id);

System.out.println("id="+id);

System.out.println("position="+position);

}

}

package com.example.wenandroid;

import java.util.ArrayList;

import java.util.HashMap;

import android.app.ListActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.ListView;

import android.widget.SimpleAdapter;

public class ListViewMain extends ListActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.listview);

ArrayList<HashMap<String,String>> list=new ArrayList<HashMap<String,String>>();

HashMap<String,String> map1=new HashMap<String,String>();

HashMap<String,String> map2=new HashMap<String,String>();

map1.put("user_name", "张三");

map1.put("user_ip", "192.168.0.110");

map2.put("user_name", "李四");

map2.put("user_ip", "192.168.0.111");

list.add(map1);

list.add(map2);

SimpleAdapter listAdapter=new SimpleAdapter(this, list, R.layout.user, new String[]{"user_name","user_ip"}, new int[]{R.id.user_name,R.id.user_ip});

setListAdapter(listAdapter);

}

protected void onListItemClick(ListView l,View v,int position,long id){

super.onListItemClick(l, v, position, id);

System.out.println("id="+id);

System.out.println("position="+position);

}

}

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港