【问题标题】:Android arrayAdapter and BaseAdapter getContext ? why getContext works on array adapter but not on base adapter?Android arrayAdapter 和 BaseAdapter getContext ?为什么 getContext 适用于数组适配器但不适用于基本适配器?
【发布时间】:2016-08-07 14:49:14
【问题描述】:

为什么 getContext() 方法在 getView 方法中的 ArrayAdapter 上起作用。

convertView = LayoutInflater.from(getContext()).inflate(R.layout.row,parent,false);

为什么相同的代码可以在 BaseAdapter 上运行?

扩展 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 类扩展 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 有一个 getContext 方法,但 BaseAdapter 没有。只需自己制作一个并返回context 或直接使用context
  • 为什么 baseadapter 没有 getcontext 方法?不是更有帮助吗?还是因为它更多的是抽象类?
  • 它更像是一个抽象类。它旨在提供实现适配器所需的基本方法。并非每个适配器都可能需要上下文,因此为了使其更精简,他们只需将其省略即可。

标签: android listview android-adapter


【解决方案1】:

好吧,ArrayAdapter 扩展了BaseAdapter 并在其构造函数中有一个context 变量,该变量已保存并可使用getContext() 访问。而BaseAdapter 有一个空的构造函数。你不提供一个,所以你不能从基类中得到它。如果需要,请像 ArrayAdapter 类一样创建自己的构造函数,并将 context 保存在那里。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多