1

I am trying to make an Activity that basically holds a list on song items that are "semi-complex." The output of one list item can be found here. I found what seemed like a good example online to follow to create such a thing, and that could is below.

However, ArrayAdapters apparently can only be used on SIMPLE TextViews (this is basically the error I am getting from the LogCat), and I have 3 TextViews I have to control and the color in one of them along with an image. So the code will not compile. I have looked into ListAdapters, but the underlining structure is completely different from the ArrayAdapter is is far from a 1 to 1 translation to it.

What should I do, and use? I am lost right now!

FYI, in the LogCat I get the following (NOTE I removed the LogCat code from the code below...that was put in right AFTER the super call in the constructor of SimpleSongAdapter)...

enter image description here

Here is my SimpleSongAdapter class

public class SimpleSongAdapter extends ArrayAdapter<SimpleSong> {

    Context context;
    int layoutResourceID;
    SimpleSong song[] = null;

    public SimpleSongAdapter(Context context, int resource, SimpleSong[] data) {
        super(context, resource, data);

        this.layoutResourceID = resource;
        this.context = context;
        this.song = data;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        super.getView(position, convertView, parent);

        View row = convertView;
        SimpleSongHolder songHolder = null;

        if (row == null) {

            LayoutInflater newView = ((Activity) context).getLayoutInflater();
            row = newView.inflate(this.layoutResourceID, parent, false);

            ExtractLayoutResources(parent, row, songHolder);

            row.setTag(songHolder);

        } else {

            songHolder = (SimpleSongHolder) row.getTag();

        }

        SetLayoutResource(songHolder, position);

        return row;

    }

    private void SetLayoutResource(SimpleSongHolder songHolder, int position) {

        SimpleSong currentSong = song[position];

        songHolder.imgSongThumbnail.setImageResource(currentSong.thumbnail);
        songHolder.txtSongName.setText(currentSong.songName);
        songHolder.txtGroupName.setText(currentSong.groupName);

        String metaText = "";
        int colorID = 0;

        // switch statement to set metaText and colorID

        songHolder.txtSongMetaInfo.setText(metaText);
        songHolder.txtSongMetaInfo.setTextColor(colorID);
    }

    private void ExtractLayoutResources(ViewGroup parent, View row, SimpleSongHolder songHolder) {

        songHolder = new SimpleSongHolder();

        songHolder.imgSongThumbnail = (ImageView) row.findViewById(R.id.imgSongThumbnail);
        songHolder.txtSongName = (TextView) row.findViewById(R.id.txtSongName);
        songHolder.txtGroupName = (TextView) row.findViewById(R.id.txtGroupName);
        songHolder.txtSongMetaInfo = (TextView) row.findViewById(R.id.txtSongMetaInfo);

    }

    private static class SimpleSongHolder {

        ImageView imgSongThumbnail;
        TextView txtSongName;
        TextView txtGroupName;
        TextView txtSongMetaInfo;

    }
}

In SimpleSong class I have

public class SimpleSong {

public int thumbnail;
    public String songName;
    public String groupName;
    public String songMetaInfo;

    public RemixType remixType;

    public SimpleSong(String songName, String groupName) {

        this.songName = songName;
        this.groupName = groupName;

    }

// Sets to set other attributes

In MainActivity I have

public class MainActivity extends Activity {

    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SimpleSong songs[] = new SimpleSong[] { 
            // new SimpleSong mock object list
        };

        listView = (ListView) findViewById(R.id.songView);

        View listHeader = (View) getLayoutInflater().inflate(R.layout.list_header, null);
        SimpleSongAdapter songAdapter = new SimpleSongAdapter(this, R.layout.list_item, songs);

        listView.addHeaderView(listHeader);
        listView.setAdapter(songAdapter);

    }
Community
  • 1
  • 1
Christopher Rucinski
  • 4,737
  • 2
  • 27
  • 58

1 Answers1

2

You are invoking this constructor:

public ArrayAdapter (Context context, int resource, T[] objects)

Which expects the resource id to be the id of a TextView, but you are passing the id of the layout.

You need to use the following constructor in stead:

public ArrayAdapter (Context context, int resource, int textViewResourceId, T[] objects)

And call it like:

SimpleSongAdapter songAdapter = new SimpleSongAdapter(this, R.layout.list_item, 0, songs);
MH.
  • 45,303
  • 10
  • 103
  • 116
prijupaul
  • 2,076
  • 2
  • 15
  • 17
  • I got it to work another way (using a BaseAdapter), but would this work still? I have 3 different TextViews that need to be populated...and one of them needs there text color modified. – Christopher Rucinski Sep 19 '13 at 01:44
  • 1
    BaseAdapter should be the ideal way to go. This should still work provided as we are ignoring the textview parameter and populating the 3 textviews in getView.I haven't tried this solution but just my thought. – prijupaul Sep 19 '13 at 01:59
  • I posted another question that relates to this one, but is with the layout issues....not a lot of activity on it right now - http://stackoverflow.com/questions/18884937/android-custom-control-layout-troubles – Christopher Rucinski Sep 19 '13 at 02:37