ListView

来源:互联网 发布:派拉软件怎么样 编辑:程序博客网 时间:2024/05/22 04:38

1.在main.xml布局文件中设置一个listview

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
  
       <!--  android:id="@+id/listLinearLayout"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"> -->
        <ListView
            android:id="@id/android:list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:drawSelectorOnTop="false"
            android:scrollbars="vertical"><!-- 当超出activity时滚动条 -->
        </ListView>   
  
</LinearLayout>

 

2.新建一个item.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:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="10dip"
    android:paddingRight="10dip"
    android:paddingTop="1dip"
    android:paddingBottom="1dip">
  <TextView
      android:id="@+id/user_name"
      android:layout_width="180dip"
      android:layout_height="30dip"
      android:textSize="10pt"
      android:singleLine="true"/>
  <TextView
      android:id="@+id/user_ip"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:textSize="10pt"
      android:gravity="right"/>
</LinearLayout>

3.完整代码

package com.example.listviewdemo;

import java.util.ArrayList;
import java.util.HashMap;

import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.view.Menu;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
//继承ListActivity
public class MainActivity extends ListActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        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>();
        HashMap<String,String> map3 = new HashMap<String, String>();
        map1.put("user_name","zhangsan");
        map1.put("user_ip","192.168.1.29");
        map2.put("user_name","lisi");
        map2.put("user_ip","192.168.1.129");
        map3.put("user_name","wangwu");
        map3.put("user_ip","192.168.1.229");
        list.add(map1);
        list.add(map2);
        list.add(map3);
        //为适配器添加数据
        SimpleAdapter adapter = new SimpleAdapter(
          this, //上下文对象
          list, //存放数据
          R.layout.item,//布局文件
          new String[]{"user_name","user_ip"}, //当前list有几个元素,列的名字,键
          new int[]{R.id.user_name,R.id.user_ip});//数据显示位置
        setListAdapter(adapter);
    }

 @Override
 protected void onListItemClick(ListView l, View v, int position, long id) {
  // TODO Auto-generated method stub
  super.onListItemClick(l, v, position, id);
  Toast.makeText(getApplicationContext(), "第"+position+" "+id+"个被选中",Toast.LENGTH_SHORT).show();
 }
   
   
}

4.显示效果

原创粉丝点击