Android中的ListView使用方法

本文介绍了一个使用 Android 的 ListActivity 来展示列表项的例子。通过继承 ListActivity 并配置相应的布局文件,可以轻松实现一个带有自定义视图的列表。代码展示了如何创建一个简单的 MP3 播放器界面,包括初始化组件、填充数据以及设置适配器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、继承ListActivity

package cn.edu.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.SimpleAdapter;

public class Mp3PlayerActivity extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
     
      List<Map<String,String>> list=new ArrayList<Map<String,String>>();
      Map<String,String> map1=new HashMap<String,String>();
      map1.put("name", "Hello");
      map1.put("size", "1000");
      list.add(map1);
      
      SimpleAdapter simpleAdapter=new SimpleAdapter(this,list,R.layout.mylistview,new String []{"name","size"},new int [] {R.id.name,R.id.size});
      setListAdapter(simpleAdapter);
      
    }
}

 二、配置main.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
  
   
    <ListView android:id="@id/android:list"  
            android:drawSelectorOnTop="false"  
            android:layout_width="fill_parent"   
            android:layout_height="wrap_content"   
            android:scrollbars="vertical"/>  
  
   
</LinearLayout>
 

三、增加mylistview.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
  <TextView
  	android:id="@+id/name"  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
    <TextView  
    android:id="@+id/size"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />

   
</LinearLayout>
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值