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