2016-03-22 4 views
0

У меня есть класс как статический с некоторыми атрибутами, класс статичен, но я получаю ошибку, что поля не являются статическими.Нестатическое поле не может ссылаться из статического контекста

Я не могу найти решение, примеры, которые я вижу, очень похожи. Держатель зрения всегда находятся внутри адаптера, как статический или общественного статики

Вот мой 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); 
    } 
} 

Буду признателен за вашу помощь. Заранее спасибо.

ответ

5
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 это имя класса. Экземпляр имеет имя viewHolder (строчная первая буква). Поля noteTitle и т. Д. Являются членами экземпляра и должны упоминаться со ссылкой на экземпляр (т. Е. viewHolder).

Смежные вопросы

 Смежные вопросы