Rec_Mervyn
MervynLammm
采纳率50%
2015-12-29 15:07

关于安卓button的findviewbyid导致程序报错,和adapter的使用错误

已采纳

package com.example.mp3test;

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

import android.app.Activity;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.SimpleAdapter;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TabWidget;

public class MainActivity extends Activity {
private TabHost tabHost;
private TabSpec specSongs;
private TabSpec specArtists;
private TabSpec specAlbums;
private TabSpec specPlaylist;

private MediaPlayer player;
private SeekBar audioSeekbar;

private Button playBtn;
private Button pauseBtn;
private Button nextBtn;
private Button preBtn;
private Button stopBtn;

private ListView songsList;
private ListView artistsList;
private ListView albumsList;
private ListView playList;

private List<Map<String,Object>> dataList;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tabHost = (TabHost) findViewById(android.R.id.tabhost);
    tabHost.setup();
    audioSeekbar = (SeekBar) findViewById(R.id.seekBar1);

// playBtn = (Button) findViewById(R.id.imagebstart);
// nextBtn = (Button) findViewById(R.id.imagebnext);
// preBtn = (Button) findViewById(R.id.imagebpre);
// stopBtn = (Button) findViewById(R.id.imagebstop);
songsList = (ListView) findViewById(R.id.listSongs);
artistsList = (ListView) findViewById(R.id.listArtists);
albumsList = (ListView) findViewById(R.id.listAlbums);
playList = (ListView) findViewById(R.id.listPlaylist);
dataList = new ArrayList>();
//init();
//set_tab_style();
add_tabs();
//set_Adapter();
}

private void init(){
    tabHost = (TabHost) findViewById(android.R.id.tabhost);
    audioSeekbar = (SeekBar) findViewById(R.id.seekBar1);
    playBtn = (Button) findViewById(R.id.imagebstart);
    nextBtn = (Button) findViewById(R.id.imagebnext);
    preBtn = (Button) findViewById(R.id.imagebpre);
    stopBtn = (Button) findViewById(R.id.imagebstop);
    songsList = (ListView) findViewById(R.id.listSongs);
    artistsList = (ListView) findViewById(R.id.listArtists);
    albumsList = (ListView) findViewById(R.id.listAlbums);
    playList = (ListView) findViewById(R.id.listPlaylist);
    dataList = new ArrayList<Map<String,Object>>();
}

private void set_Adapter(){
    setListAdpter(songsList, getMp3Infos("songs"));
    setListAdpter(artistsList, getMp3Infos("artists"));
    setListAdpter(albumsList, getMp3Infos("albums"));
    setListAdpter(playList, getMp3Infos("playlist"));
}

private void set_tab_style(){
    LinearLayout layout = (LinearLayout)tabHost.getChildAt(0);
    TabWidget tw = (TabWidget)layout.getChildAt(0);
}

private void add_tabs(){
    specSongs = tabHost.newTabSpec("Tab 1");
    specSongs.setContent(R.id.tabSongs);
    specSongs.setIndicator("Songs");
    tabHost.addTab(specSongs);

    specArtists = tabHost.newTabSpec("Tab 2");
    specArtists.setContent(R.id.tabArtists);
    specArtists.setIndicator("Artists");
    tabHost.addTab(specArtists);

    specAlbums = tabHost.newTabSpec("Tab 3");
    specAlbums.setContent(R.id.tabAlbums);
    specAlbums.setIndicator("Albums");
    tabHost.addTab(specAlbums);

    specPlaylist = tabHost.newTabSpec("Tab 4");
    specPlaylist.setContent(R.id.tabPlaylist);
    specPlaylist.setIndicator("Playlist");
    tabHost.addTab(specPlaylist);
}

public List<Mp3Info> getMp3Infos(String c){
    Cursor cursor = null;
    if(c.equals("songs")){
        cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                null, null, null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
    }
    else if(c.equals("artists")){
        cursor = getContentResolver().query(MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI,
                null, null, null, MediaStore.Audio.Artists.DEFAULT_SORT_ORDER);
    }
    else if(c.equals("albums")){
        cursor = getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
                null, null, null, MediaStore.Audio.Albums.DEFAULT_SORT_ORDER);
    }
    else if(c.equals("playlist")){
        cursor = getContentResolver().query(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
                null, null, null, MediaStore.Audio.Playlists.DEFAULT_SORT_ORDER);
    }
    List<Mp3Info> mp3Infos = new ArrayList<Mp3Info>();
    for(int i = 0; i < cursor.getCount(); ++i){
        Mp3Info mp3info = new Mp3Info();
        cursor.moveToNext();
        String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
        String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
        String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
        long duration = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
        int isMusic = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.IS_MUSIC));
        if(isMusic != 0){
            mp3info.title = title;
            mp3info.album = album;
            mp3info.artist = artist;
            mp3info.duration = duration;
            mp3Infos.add(mp3info);
        }
    }
    return mp3Infos;
}

public void setListAdpter(ListView mMusiclist, List<Mp3Info> mp3Infos) {
    for (Iterator iterator = mp3Infos.iterator(); iterator.hasNext();) {
        Mp3Info mp3Info = (Mp3Info) iterator.next();
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("title", mp3Info.getTitle());
        map.put("Artist", mp3Info.getArtist());
        map.put("pic", R.drawable.music);
        dataList.add(map);
    }
    SimpleAdapter mAdapter = new SimpleAdapter(this, dataList,
            R.layout.item, new String[] { "title", "Artist", "pic" },
            new int[] {R.id.itemsongs, R.id.itemArtists, R.id.itempic });
    mMusiclist.setAdapter(mAdapter);      
}  

public class Mp3Info{
    private String title;
    private String album;
    private String artist;
    private long duration;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAlbum() {
        return album;
    }

    public void setAlbum(String album) {
        this.album = album;
    }

    public String getArtist() {
        return artist;
    }

    public void setArtist(String artist) {
        this.artist = artist;
    }

    public long getDuration() {
        return duration;
    }

    public void setDuration(long duration) {
        this.duration = duration;
    }
}

}


以上代码,谢谢

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

1条回答

相关推荐