我有以下活动类:
public class Homescreen extends Activity implements OnClickListener {
ImageButton play, repeat, previous, next, playlist, shuffle;
Intermediate inter;
ListView LVSongs;
Toast t1;
ArrayAdapter<HashMap<String, String>> songsAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.homescreen);
initialize();
initializeListeners();
LVSongs.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
});
new loadmusicfiles().execute("vivek");
}
private void initializeListeners() {
// TODO Auto-generated method stub
play.setOnClickListener(this);
repeat.setOnClickListener(this);
previous.setOnClickListener(this);
shuffle.setOnClickListener(this);
playlist.setOnClickListener(this);
next.setOnClickListener(this);
}
private void initialize() {
// TODO Auto-generated method stub
play = (ImageButton) findViewById(R.id.IBPlay);
repeat = (ImageButton) findViewById(R.id.IBRepeat);
previous = (ImageButton) findViewById(R.id.IBPrevious);
next = (ImageButton) findViewById(R.id.IBNext);
playlist = (ImageButton) findViewById(R.id.IBPlaylist);
shuffle = (ImageButton) findViewById(R.id.IBShuffle);
LVSongs = (ListView) findViewById(R.id.LVplaylist);
songsAdapter = new ArrayAdapter<HashMap<String, String>>(this,
android.R.layout.simple_list_item_1, Serviceman.songs);
LVSongs.setAdapter(songsAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.homescreen, menu);
return true;
}
@Override
public void onClick(View clickedVariable) {
// TODO Auto-generated method stub
switch (clickedVariable.getId()) {
case R.id.IBPlay:
break;
case R.id.IBNext:
break;
case R.id.IBPrevious:
break;
case R.id.IBPlaylist:
break;
case R.id.IBRepeat:
break;
case R.id.IBShuffle:
break;
}
}
public void ShowToast(String texttoshow) {
t1 = Toast.makeText(this, texttoshow, Toast.LENGTH_SHORT);
t1.show();
}
public class loadmusicfiles extends AsyncTask<String, Integer, String> {
ProgressDialog dialog;
protected void onPreExecute() {
dialog = new ProgressDialog(Homescreen.this);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
// dialog.setMax(100);
dialog.show();
}
protected void onProgressUpdate(Integer... progress) {
dialog.incrementProgressBy(progress[0]);
}
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
HashMap<String, String> song;
Uri allsongsuri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String[] STAR = { "*" };
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
@SuppressWarnings("deprecation")
Cursor cursor = managedQuery(allsongsuri, STAR, selection, null,
null);
try {
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
song = new HashMap<String, String>();
// SongName
String song_name = cursor
.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
// path
String fullpath = cursor
.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.DATA));
song.put("songtitle", song_name);
song.put("songpath", fullpath);
Serviceman.songs.add(song);
songsAdapter.notifyDataSetChanged();
publishProgress(5);
} while (cursor.moveToNext());
}
}
} catch (NullPointerException exp) {
Log.e("~~Null Pointer Exception~~", exp.toString());
android.util.Log.e("->>", "~~stacktrace~~", exp);
} catch (Exception exp) {
Log.e("~~Exception~~", exp.toString());
}
dialog.dismiss();
return "Vivek";
}
protected void onPostExecute(String result) {
ShowToast("Fininshed Search");
}
}
注意:我的静态 arraylist '歌曲'在另一个班级中声明并初始化了军人我可以使用相同的变量来播放歌曲UI不可用,但是UI有播放列表 - &gt; LISTVIEW 来显示卡片中包含的所有歌曲......令人惊讶的是它没有显示什么都空白.....
Morevoer正在使用框架和滑动抽屉来显示此活动中的播放列表,而不是完全创建不同的活动!!