zoukankan      html  css  js  c++  java
  • listView 结合 ArrayList和HashMap 的应用

    music_item.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width
    ="fill_parent"
    android:layout_height
    ="fill_parent"
    android:orientation
    ="horizontal"
    android:paddingLeft
    ="10dip"
    android:paddingRight
    ="10dip"
    android:paddingTop
    ="1dip"
    android:paddingBottom
    ="1dip">

    <TextView

    android:layout_width="wrap_content"
    android:layout_height
    ="wrap_content"
    android:id
    ="@+id/mp3_item"
    />
    <TextView android:id="@+id/mp3_name"
    android:layout_height
    ="30dip"
    android:layout_width
    ="fill_parent"
    android:textSize
    ="10pt"
    android:paddingLeft
    ="10dp"
    android:textColor
    ="#FFFFFF"/>
    </LinearLayout>

    列表

    /**
    * 文件过滤器
    *
    *
    @author
    *
    */
    class MusicFilter implements FilenameFilter {

    @Override
    public boolean accept(File dir, String filename) {

    return (filename.endsWith(".mp3"));
    }

    }

    //绑定音乐
    void musicList(){
    try{
    musiclist.clear();
    //扫描SD卡下MP3文件夹中的Mp3歌曲,将扫描出的MP3对象存放到集合中
    File mp3File=new File(SkyMusicActivity.SDPath);
    if(mp3File.listFiles(new MusicFilter()).length>=0){
    for(File file:mp3File.listFiles(new MusicFilter())){
    Mp3 mp3=new Mp3();
    String[] str=file.getName().split("\\.");
    mp3.setMp3_name(str[0]);
    mp3.setId(i);
    i++;
    musiclist.add(mp3);
    }
    }
    }
    catch(Exception ex)
    {
    ex.printStackTrace();
    }
    }

    @Override
    protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    HashMap<String, String> map=null;
    List<HashMap<String, String>> list=new ArrayList<HashMap<String,String>>();

    if(musiclist.size()!=0){
    for(Mp3 m:musiclist){
    map=new HashMap<String, String>();
    map.put("mp3_item", Integer.toString(m.getId()+1));
    map.put("mp3_name", m.getMp3_name());
    list.add(map);
    }
    SimpleAdapter adapter=new SimpleAdapter
    (this, list, R.layout.music_item, new String[]{"mp3_item","mp3_name"}, new int[]{R.id.mp3_item,R.id.mp3_name});
    setListAdapter(adapter);
    }
    }


    实现后的界面

  • 相关阅读:
    模块化编程
    flex 弹性布局
    作用域与作用域链
    深入解读JavaScript面向对象编程实践
    javascript Null、Undefined 、NaN的联系与区别
    跨域常见解决方案
    Reverse Pairs
    315. Count of Smaller Numbers After Self
    2. Add Two Numbers
    657. Judge Route Circle
  • 原文地址:https://www.cnblogs.com/shanshan520/p/2390541.html
Copyright © 2011-2022 走看看