Android arrayAdapter 和 BaseAdapter getContext ?为什么 getContext 适用于数组适配器而不适用于基本适配器?
Android arrayAdapter and BaseAdapter getContext ? why getContext works on array adapter but not on base adapter?
为什么 getContext() 方法在 getView 方法中的 ArrayAdapter 上起作用。
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row,parent,false);
为什么相同的代码可以在 BaseAdapter 上工作?
代码 Class 扩展 ArrayAdapter ?
package com.example.manis.mylist3;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by Manis on 8/6/2016.
*/
public class NoteAdapter extends ArrayAdapter{
public NoteAdapter(Context context, ArrayList<Note> notes) {
super(context, 0 ,notes);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Log.v("adapter","called"+position);
Note note = (Note) getItem(position);
if(convertView==null){
// Log.v("noteAdapter","ConvertView was null");
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row,parent,false);
}
TextView noteTitle= (TextView) convertView.findViewById(R.id.rowTitle);
TextView noteSubText= (TextView) convertView.findViewById(R.id.rowSubText);
ImageView img = (ImageView) convertView.findViewById(R.id.rowImg);
noteTitle.setText(note.getTitle());
noteSubText.setText(note.getSubtext());
img.setImageResource(note.getAssociatedDrawable());
return convertView;
}
}
NoteBaseAdapter 代码Class 扩展 BaseAdapter
package com.example.manis.mylist3;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import java.util.ArrayList;
public class NoteBaseAdapter extends BaseAdapter {
ArrayList<Note> notes;
Context context;
public NoteBaseAdapter(Context c, ArrayList<Note> notes)
{
this.context=c;
this.notes=notes;
}
@Override
public int getCount() {
return notes.size();
}
@Override
public Object getItem(int position) {
return notes.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//error
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row,parent,false);
}
}
好吧,ArrayAdapter
扩展了 BaseAdapter
并在其构造函数中有一个 context
变量,该变量被保存并可以使用 getContext()
访问。而 BaseAdapter
有一个空的构造函数。你不给它提供一个,所以你不能从基地class得到它。如果您需要一个,请像 ArrayAdapter
class 那样制作您自己的构造函数并将您的 context
保存在那里。
为什么 getContext() 方法在 getView 方法中的 ArrayAdapter 上起作用。
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row,parent,false);
为什么相同的代码可以在 BaseAdapter 上工作?
代码 Class 扩展 ArrayAdapter ?
package com.example.manis.mylist3;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by Manis on 8/6/2016.
*/
public class NoteAdapter extends ArrayAdapter{
public NoteAdapter(Context context, ArrayList<Note> notes) {
super(context, 0 ,notes);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Log.v("adapter","called"+position);
Note note = (Note) getItem(position);
if(convertView==null){
// Log.v("noteAdapter","ConvertView was null");
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row,parent,false);
}
TextView noteTitle= (TextView) convertView.findViewById(R.id.rowTitle);
TextView noteSubText= (TextView) convertView.findViewById(R.id.rowSubText);
ImageView img = (ImageView) convertView.findViewById(R.id.rowImg);
noteTitle.setText(note.getTitle());
noteSubText.setText(note.getSubtext());
img.setImageResource(note.getAssociatedDrawable());
return convertView;
}
}
NoteBaseAdapter 代码Class 扩展 BaseAdapter
package com.example.manis.mylist3;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import java.util.ArrayList;
public class NoteBaseAdapter extends BaseAdapter {
ArrayList<Note> notes;
Context context;
public NoteBaseAdapter(Context c, ArrayList<Note> notes)
{
this.context=c;
this.notes=notes;
}
@Override
public int getCount() {
return notes.size();
}
@Override
public Object getItem(int position) {
return notes.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//error
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row,parent,false);
}
}
好吧,ArrayAdapter
扩展了 BaseAdapter
并在其构造函数中有一个 context
变量,该变量被保存并可以使用 getContext()
访问。而 BaseAdapter
有一个空的构造函数。你不给它提供一个,所以你不能从基地class得到它。如果您需要一个,请像 ArrayAdapter
class 那样制作您自己的构造函数并将您的 context
保存在那里。