我正在尝试创建一个基本上包含“半复杂”歌曲项目列表的活动。可以找到一个列表项的输出here。我发现在网上创建这样的东西似乎是一个很好的例子,可能就在下面。
然而,ArrayAdapters显然只能在SIMPLE TextViews上使用(这基本上是我从LogCat得到的错误),我有3个TextViews我必须控制,其中一个颜色和图像一起。所以代码不会编译。我已经研究过ListAdapters,但是下划线结构与ArrayAdapter完全不同,它远不是1到1的转换。
我该怎么做,并使用?我现在迷路了!
仅供参考,在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);
}
答案 0 :(得分:2)
您正在调用此构造函数:
public ArrayAdapter (Context context, int resource, T[] objects)
期望资源ID是TextView
的id,但是你传递了布局的id。
您需要使用以下构造函数:
public ArrayAdapter (Context context, int resource, int textViewResourceId, T[] objects)
并称之为:
SimpleSongAdapter songAdapter = new SimpleSongAdapter(this, R.layout.list_item, 0, songs);