weixin_42491708
A范县百盛商贸
2011-11-19 11:19

Android中ListView如何使用,求源码?

已采纳

[size=large]Android中ListView如何使用,求源码?
请详细点,最好能够直接运行的,谢谢[/size]

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • shenjichao2008 shenjichao2008 10年前

    [color=red]user.xml
    <?xml version="1.0" encoding="utf-8"?>
    android:orientation="horizontal" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <!-- 用户名文本 -->
    <TextView android:id="@+id/userNameId" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:textSize="10pt"
        android:gravity="left" />
    
    <!-- IP文本 -->
    <TextView android:id="@+id/ipId" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:textSize="10pt"
        android:gravity="right" />
    

    main.xml
    <?xml version="1.0" encoding="utf-8"?>
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:orientation="vertical"
        android:paddingLeft="10px" android:paddingRight="10px"
        android:paddingTop="10px" android:paddingBottom="10px">
    
        <ListView android:id="@id/android:list" android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:scrollbars="vertical"
            android:drawSelectorOnTop="true" />
    
    </LinearLayout>
    

    ListViewActivity.java
    package com.duoguo.android;

    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;

    /**

    • ListView的使用
    • @author shyboy(897948924@qq.com)
    • */
      public class ListViewActivity extends ListActivity {

      @Override
      public void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      
      ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();// 实例化ArrayList链表,用来存放HashMap对象
      
      // 实例化HashMap对象,用来存放用户名和IP
      HashMap<String, String> map1 = new HashMap<String, String>();
      map1.put("user_name", "shyboy");
      map1.put("user_ip", "192.168.0.1");
      HashMap<String, String> map2 = new HashMap<String, String>();
      map2.put("user_name", "playboy");
      map2.put("user_ip", "192.168.0.2");
      HashMap<String, String> map3 = new HashMap<String, String>();
      map3.put("user_name", "simplyboy");
      map3.put("user_ip", "192.168.0.3");
      
      // 添加HashMap集合到ArrayList链表中
      list.add(map1);
      list.add(map2);
      list.add(map3);
      
      SimpleAdapter listSimpleAdapter = new SimpleAdapter(this, list,
              R.layout.user, new String[] { "user_name", "user_ip" },
              new int[] { R.id.userNameId, R.id.ipId });// 实例化适配器
      this.setListAdapter(listSimpleAdapter);
      

      }

      // 当点击ListView内容时触发该事件
      @Override
      protected void onListItemClick(ListView l, View v, int position, long id) {

      super.onListItemClick(l, v, position, id);
      System.out.println("id is:" + id);
      System.out.println("position is:" + position);
      

      }

    }

    希望对你有所帮助!!![/color]

    点赞 评论 复制链接分享

相关推荐