0

我需要(除非您能想到更好的方法)将多个对象传递给自定义列表适配器。我知道我在这里叫错了树,如果有人让我走上正确的道路,我将不胜感激!

谢谢

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
4

2 回答 2

3

1)第一行说您需要将对象传递给适配器。您的代码表明您正在这样做。所以你隐含的问题是模棱两可的。

2)它不起作用,因为您在构造函数中传递了 null (而不是列表)。

super(context, null, resource, from, to);

这是预期的:super(Context, List<Map<String, ?>>, int, String[], int[])

3) 您在适配器的构造函数中传递的 null 会导致简单适配器的内部 List 为 null。它为 null 意味着 SimpleAdapter.getCount() 没有对象(列表)来获取数量。

于 2012-10-26T22:48:56.520 回答
0

AedonEtLIRA,感谢您的帮助。最后我选择更改SimpleAdapterArrayAdapter.

public class MyPlayListAdapter extends ArrayAdapter<Song> {

private ArrayList<Song> songsList;
private ArrayList<Song> playlistcheck = new ArrayList<Song>();
private ArrayList <Song> retained_songsList = new ArrayList<Song>();
private String folderMode;
private String TAG = "AndroidMediaCenter";



public MyPlayListAdapter(Context context, int textViewResourceId, ArrayList<Song> songsList, ArrayList<Song> retained_songsList, String folderMode) {
        super(context, textViewResourceId, songsList);
        this.songsList = songsList;
        this.retained_songsList = retained_songsList;
        this.folderMode = folderMode;
}

从我的主要活动中调用:

playlistadapter = new MyPlayListAdapter(MyApplication.getAppContext(),
                                        R.layout.file_view, 
                                        songsList, 
                                        retained_songsList, 
                                        folderMode);
于 2012-10-27T08:36:15.777 回答