Android Multiple objects in SimpleAdapter [closed]

2019-08-19 03:11发布

I have a need (unless you can think of a better way) of passing multiple objects to a custom list adapter. I know that I'm barking up the wrong tree here, and would appreciate someone setting me on the right course!

Thanks

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 });

And my adapter class:

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;
             }



      }

However, this doesn't inflate, throwing the following errors:

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

2条回答
smile是对你的礼貌
2楼-- · 2019-08-19 03:45

AedonEtLIRA, thanks for your help. In the end I opted to change the SimpleAdapter to ArrayAdapter.

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;
}

called from my main activity by:

playlistadapter = new MyPlayListAdapter(MyApplication.getAppContext(),
                                        R.layout.file_view, 
                                        songsList, 
                                        retained_songsList, 
                                        folderMode);
查看更多
不美不萌又怎样
3楼-- · 2019-08-19 03:47

1) The first line says that you needs to pass object into the adapter. Your code says that you are doing so. So your implied question is ambiguous.

2) It's not working because you passed null (instead of a list) in the constructor.

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

This is what is expected: super(Context, List<Map<String, ?>>, int, String[], int[])

3) The null that you passed in your adapter's constructor causes the internal List for simple adapter to be null. It being null means that SimpleAdapter.getCount() has no object (list) to get the quantity of.

查看更多
登录 后发表回答