我需要(除非您能想到更好的方法)将多个对象传递给自定义列表适配器。我知道我在这里叫错了树,如果有人让我走上正确的道路,我将不胜感激!
谢谢
playlistadapter = new MyPlaylistAdapter(MyApplication.getAppContext(),
songsList,
retained_songsList,
folderMode,
R.layout.file_view,
new String[] { "songTitle","songAlbum", "songPath" },
new int[] { R.id.checkTextView, R.id.text2, R.id.text3 });
还有我的适配器类:
public class MyPlaylistAdapter extends SimpleAdapter{
private ArrayList <Song> songsList = new ArrayList<Song>();
private ArrayList <Song> retained_songsList = new ArrayList<Song>();
private ArrayList<Song> playlistcheck = new ArrayList<Song>();
private String folderMode;
private String TAG = "AndroidMediaCenter";
public MyPlaylistAdapter(Context context,List<Song> SongsList, List<Song> Retained_songsList, String FolderMode,int resource, String[] from, int[] to) {
super(context, null, resource, from, to);
songsList.clear();
songsList.addAll(SongsList);
Log.i(TAG, "MyPlayListAdapter Songslist = " + songsList.size());
retained_songsList.clear();
retained_songsList.addAll(Retained_songsList);
folderMode = FolderMode;
}
public View getView(int position, View convertView, ViewGroup parent) {
//PlayListViewHolder holder;
CheckedTextView checkTextView;
TextView text2;
TextView text3;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) MyApplication.getAppContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//LayoutInflater inflater=getLayoutInflater();
convertView=inflater.inflate(R.layout.file_view, parent, false);
//convertView.setBackgroundColor(0xFF00FF00 );
//holder = new PlayListViewHolder();
checkTextView = (CheckedTextView) convertView.findViewById(R.id.checkTextView);
text2 = (TextView) convertView.findViewById(R.id.text2);
text3 = (TextView) convertView.findViewById(R.id.text3);
//convertView.setTag(holder);
} else {
//holder = (PlayListViewHolder) convertView.getTag();
}
//put something into textviews
String tracks = null;
String tracks_Details = null;
String trackspath = null;
tracks = songsList.get(position).getSongTitle();
tracks_Details = songsList.get(position).getAlbum() + " (" + songsList.get(position).getArtist() + ")";
trackspath = songsList.get(position).getSongPath();
checkTextView = (CheckedTextView) convertView.findViewById(R.id.checkTextView);
text2 = (TextView) convertView.findViewById(R.id.text2);
text3 = (TextView) convertView.findViewById(R.id.text3);
checkTextView.setText(tracks);
if(folderMode.equals("Playlists")){
checkTextView.setBackgroundColor(Color.GREEN);
checkTextView.setChecked(false);
try {
int listsize_rs = retained_songsList.size();
for (int j = 0; j<listsize_rs;j++){
if((retained_songsList.get(j).getSongPath()).equals(songsList.get(position).getSongPath())){
checkTextView.setBackgroundColor(Color.TRANSPARENT);
//Need to check here whether the checkedtextview is ticked or not
checkTextView.setChecked(true);
playlistcheck.add(songsList.get(position));
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}else
{
//Need to check here whether the checkedtextview is ticked or not
try {
if (songsList.get(position).getSongCheckedStatus()==true){
checkTextView.setChecked(true);
}else{
checkTextView.setChecked(false);
}
} catch (Exception e) {
e.printStackTrace();
}
}
text2.setText(tracks_Details);
text3.setText(trackspath);
Log.i(TAG, "MyPlayListAdapter Songslist = " + songsList.size());
return convertView;
}
}
但是,这不会膨胀,会引发以下错误:
10-26 23:11:09.464: E/AndroidRuntime(2826): FATAL EXCEPTION: main
10-26 23:11:09.464: E/AndroidRuntime(2826): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.GetMusicComplete flg=0x10 } in com.Nmidia.AMC.MusicActivity$18@414c5770
10-26 23:11:09.464: E/AndroidRuntime(2826): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:765)
10-26 23:11:09.464: E/AndroidRuntime(2826): at android.os.Handler.handleCallback(Handler.java:615)
10-26 23:11:09.464: E/AndroidRuntime(2826): at android.os.Handler.dispatchMessage(Handler.java:92)
10-26 23:11:09.464: E/AndroidRuntime(2826): at android.os.Looper.loop(Looper.java:137)
10-26 23:11:09.464: E/AndroidRuntime(2826): at android.app.ActivityThread.main(ActivityThread.java:4745)
10-26 23:11:09.464: E/AndroidRuntime(2826): at java.lang.reflect.Method.invokeNative(Native Method)
10-26 23:11:09.464: E/AndroidRuntime(2826): at java.lang.reflect.Method.invoke(Method.java:511)
10-26 23:11:09.464: E/AndroidRuntime(2826): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-26 23:11:09.464: E/AndroidRuntime(2826): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-26 23:11:09.464: E/AndroidRuntime(2826): at dalvik.system.NativeStart.main(Native Method)
10-26 23:11:09.464: E/AndroidRuntime(2826): Caused by: java.lang.NullPointerException
10-26 23:11:09.464: E/AndroidRuntime(2826): at android.widget.SimpleAdapter.getCount(SimpleAdapter.java:93)
10-26 23:11:09.464: E/AndroidRuntime(2826): at android.widget.ListView.setAdapter(ListView.java:460)
10-26 23:11:09.464: E/AndroidRuntime(2826): at com.Nmidia.AMC.MusicActivity.setFilterMusic(MusicActivity.java:1230)
10-26 23:11:09.464: E/AndroidRuntime(2826): at com.Nmidia.AMC.MusicActivity$18.onReceive(MusicActivity.java:996)
10-26 23:11:09.464: E/AndroidRuntime(2826): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:755)
10-26 23:11:09.464: E/AndroidRuntime(2826): ... 9 more