Android之ListView

来源:互联网 发布:office办公软件论坛 编辑:程序博客网 时间:2024/06/17 22:11

         发个ListView的例子,关于他的ID有个问题,先记着以后明白了在解决。

package com.example.listview;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 MainActivity extends ListActivity {@Overrideprotected 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", "123132");map2.put("user_name", "lisi");map2.put("user_ip","2888888");map3.put("user_name", "wangwu");map3.put("user_ip","00000999");list.add(map1);list.add(map2);    list.add(map3);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);}@Overrideprotected void onListItemClick(ListView l, View v, int position, long id) {// TODO Auto-generated method stubsuper.onListItemClick(l, v, position, id);System.out.println(id);System.out.println(position);}}

<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:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="vertical" >        <ListView            android:id="@id/android:list"            android:layout_width="match_parent"            android:layout_height="wrap_content"             android:scrollbars="vertical"            android:drawSelectorOnTop="false">        </ListView>    </LinearLayout></LinearLayout>

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal" >    <TextView        android:id="@+id/user_name"        android:layout_width="180dip"        android:layout_height="30dip"        android:text="TextView"         android:singleLine="true"        android:textSize="10pt"/>    <TextView        android:id="@+id/user_ip"        android:layout_width="180dip"        android:layout_height="30dip"        android:text="TextView"         android:singleLine="true"        android:textSize="10pt"        android:gravity="right"        /></LinearLayout>

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

ID为什么要这莫写,不这么写一直运行不了

希望有大神能够解答。

0 0
原创粉丝点击