Android Multiple objects in SimpleAdapter

Posted by Adam Sherratt on Stack Overflow See other posts from Stack Overflow or by Adam Sherratt
Published on 2012-10-26T22:15:12Z Indexed on 2012/10/26 23:00 UTC
Read the original article Hit count: 255

Filed under:
|
|

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

© Stack Overflow or respective owner

Related posts about android

Related posts about listview