不能从静态上下文中引用非静态字段
Non-static field cannot be referenced from a static context
我有一个 class 是静态的,带有一些属性,class 是静态的,但我得到的错误是字段不是静态的。
我想不出解决办法,我看到的例子看起来很相似。视图持有者始终在适配器内部,如 static 或 public static
这是我的 ArrayAdapter:
public class NoteAdapter extends ArrayAdapter<Note> {
public static class ViewHolder{
TextView noteTitle;
TextView noteBody;
ImageView noteIcon;
}
public NoteAdapter(Context context, ArrayList<Note> notes) {
super(context, 0, notes);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Note note = getItem(position);
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_row, parent, false);
// == Here I am getting the error ==
ViewHolder.noteTitle = (TextView) convertView.findViewById(R.id.text_item_note_title);
ViewHolder.noteBody = (TextView) convertView.findViewById(R.id.text_item_note_body);
ViewHolder.noteIcon = (ImageView) convertView.findViewById(R.id.image_item_note);
convertView.setTag(viewHolder);
}
return convertView;
}
}
这是我的笔记模型:
public class Note {
private String title, message;
private long noteId, dateCreateMilli;
private Category category;
public enum Category {PERSONAL, TECHNICAL, QUOTE, FINANCE}
public Note(String title, String message, Category category) {
this.title = title;
this.message = message;
this.category = category;
this.noteId = 0;
this.dateCreateMilli = 0;
}
public Note(String title, String message, Category category, long noteId, long dateCreateMilli) {
this.title = title;
this.message = message;
this.category = category;
this.noteId = noteId;
this.dateCreateMilli = dateCreateMilli;
}
public String getTitle() {
return this.title;
}
public String getMessage() {
return this.message;
}
public long getNoteId() {
return this.noteId;
}
public long getDateCreateMilli() {
return this.dateCreateMilli;
}
public Category getCategory() {
return this.category;
}
public TextDrawable getIconResource() {
return TextDrawable.builder().buildRound("P", android.R.color.darker_gray);
}
}
非常感谢您的帮助。提前致谢。
ViewHolder.noteTitle = (TextView) convertView.findViewById(R.id.text_item_note_title);
ViewHolder.noteBody = (TextView) convertView.findViewById(R.id.text_item_note_body);
ViewHolder.noteIcon = (ImageView) convertView.findViewById(R.id.image_item_note);
ViewHolder
是 class 的名称。该实例名为 viewHolder
(首字母小写)。字段 noteTitle
等是实例成员,必须通过对实例的引用来引用(即 viewHolder
)。
我有一个 class 是静态的,带有一些属性,class 是静态的,但我得到的错误是字段不是静态的。
我想不出解决办法,我看到的例子看起来很相似。视图持有者始终在适配器内部,如 static 或 public static
这是我的 ArrayAdapter:
public class NoteAdapter extends ArrayAdapter<Note> {
public static class ViewHolder{
TextView noteTitle;
TextView noteBody;
ImageView noteIcon;
}
public NoteAdapter(Context context, ArrayList<Note> notes) {
super(context, 0, notes);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Note note = getItem(position);
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_row, parent, false);
// == Here I am getting the error ==
ViewHolder.noteTitle = (TextView) convertView.findViewById(R.id.text_item_note_title);
ViewHolder.noteBody = (TextView) convertView.findViewById(R.id.text_item_note_body);
ViewHolder.noteIcon = (ImageView) convertView.findViewById(R.id.image_item_note);
convertView.setTag(viewHolder);
}
return convertView;
}
}
这是我的笔记模型:
public class Note {
private String title, message;
private long noteId, dateCreateMilli;
private Category category;
public enum Category {PERSONAL, TECHNICAL, QUOTE, FINANCE}
public Note(String title, String message, Category category) {
this.title = title;
this.message = message;
this.category = category;
this.noteId = 0;
this.dateCreateMilli = 0;
}
public Note(String title, String message, Category category, long noteId, long dateCreateMilli) {
this.title = title;
this.message = message;
this.category = category;
this.noteId = noteId;
this.dateCreateMilli = dateCreateMilli;
}
public String getTitle() {
return this.title;
}
public String getMessage() {
return this.message;
}
public long getNoteId() {
return this.noteId;
}
public long getDateCreateMilli() {
return this.dateCreateMilli;
}
public Category getCategory() {
return this.category;
}
public TextDrawable getIconResource() {
return TextDrawable.builder().buildRound("P", android.R.color.darker_gray);
}
}
非常感谢您的帮助。提前致谢。
ViewHolder.noteTitle = (TextView) convertView.findViewById(R.id.text_item_note_title);
ViewHolder.noteBody = (TextView) convertView.findViewById(R.id.text_item_note_body);
ViewHolder.noteIcon = (ImageView) convertView.findViewById(R.id.image_item_note);
ViewHolder
是 class 的名称。该实例名为 viewHolder
(首字母小写)。字段 noteTitle
等是实例成员,必须通过对实例的引用来引用(即 viewHolder
)。