我正在尝试制作一个基本上包含“半复杂”歌曲项目列表的活动。可以在此处找到一个列表项的输出。我在网上找到了一个很好的例子来创建这样的东西,这可能在下面。
但是,ArrayAdapters 显然只能在 SIMPLE TextViews 上使用(这基本上是我从 LogCat 得到的错误),而且我有 3 个 TextViews 我必须控制,其中一个的颜色以及图像。所以代码不会编译。我研究过 ListAdapters,但下划线结构与 ArrayAdapter 完全不同,远非一对一的翻译。
我应该怎么做和使用?我现在迷路了!
仅供参考,在 LogCat 中我得到以下信息(注意我从下面的代码中删除了 LogCat 代码......这是在 SimpleSongAdapter 的构造函数中的超级调用之后放置的)......
这是我的SimpleSongAdapter
课
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;
}
}
在SimpleSong
课堂上我有
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
在MainActivity
我有
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);
}