首页 > 代码库 > android控件开发之ListView
android控件开发之ListView
android控件开发之ListView
本文主要讲述安卓开发中的ListView控件的使用方法
java代码:
package com.example.listview;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//声明list,且list中的单元是map
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
//声明map,且map中的元素是String键值对
HashMap<String, String> map1 = new HashMap<String, String>();
HashMap<String, String> map2 = new HashMap<String, String>();
HashMap<String, String> map3 = new HashMap<String, String>();
//向map中put键值对数据
map1.put("name", "zhangsan");
map1.put("ip", "192.168.1.1");
map2.put("name", "lisi");
map2.put("ip", "192.168.1.2");
map3.put("name", "wangwu");
map3.put("ip", "192.168.1.3");
//将map添加到list中
list.add(map1);
list.add(map2);
list.add(map3);
//生成adapter适配器
SimpleAdapter listAdapter = new SimpleAdapter(this, list,
R.layout.user, new String[]{"name","ip"},
new int[]{R.id.name,R.id.ip});
setListAdapter(listAdapter);
}
//给list绑定监听器,当按下list中某一个单元时,就会相应此函数
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
System.out.println("id = " + id);
System.out.println("position = " + position);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
import java.util.ArrayList;
import java.util.HashMap;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//声明list,且list中的单元是map
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
//声明map,且map中的元素是String键值对
HashMap<String, String> map1 = new HashMap<String, String>();
HashMap<String, String> map2 = new HashMap<String, String>();
HashMap<String, String> map3 = new HashMap<String, String>();
//向map中put键值对数据
map1.put("name", "zhangsan");
map1.put("ip", "192.168.1.1");
map2.put("name", "lisi");
map2.put("ip", "192.168.1.2");
map3.put("name", "wangwu");
map3.put("ip", "192.168.1.3");
//将map添加到list中
list.add(map1);
list.add(map2);
list.add(map3);
//生成adapter适配器
SimpleAdapter listAdapter = new SimpleAdapter(this, list,
R.layout.user, new String[]{"name","ip"},
new int[]{R.id.name,R.id.ip});
setListAdapter(listAdapter);
}
//给list绑定监听器,当按下list中某一个单元时,就会相应此函数
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
System.out.println("id = " + id);
System.out.println("position = " + position);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
主布局文件mail.xml代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<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"
android:drawSelectorOnTop="false"
android:scrollbars="vertical"></ListView>
</LinearLayout>
</LinearLayout>
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<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"
android:drawSelectorOnTop="false"
android:scrollbars="vertical"></ListView>
</LinearLayout>
</LinearLayout>
user.xml代码实现:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mapLinear"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
tools:ignore="UselessParent" >
<TextView
android:id="@+id/name"
android:layout_width="180dip"
android:layout_height="30dip"
android:textSize="20dip"
android:gravity="center_vertical"/>
<TextView
android:id="@+id/ip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20dip"/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mapLinear"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
tools:ignore="UselessParent" >
<TextView
android:id="@+id/name"
android:layout_width="180dip"
android:layout_height="30dip"
android:textSize="20dip"
android:gravity="center_vertical"/>
<TextView
android:id="@+id/ip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20dip"/>
</LinearLayout>
实现效果如下:
点击相关的list列表,在LogCat中会输出相关的list信息如下:
android控件开发之ListView
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。