【问题标题】:Context in a baseAdapter classbaseAdapter 类中的上下文
【发布时间】:2013-03-04 10:22:10
【问题描述】:

有没有办法通过getApplicationContext()BaseAdapter 类中获取context

我需要这个,因为我要从 url 加载图像。这里我使用context

这是在扩展BaseAdapter的类中

ImageLoader imgLoader = new ImageLoader(getApplicationContext());

【问题讨论】:

  • 在自定义baseadapter的构造函数中传递它
  • 您可以使用重载的构造函数传递上下文

标签: android baseadapter android-context


【解决方案1】:

在扩展/实现时将其传递给构造函数:

public class MyAdapter extends SomeBaseAdapter{
  private final Context mcontext;

  public MyAdapter(Context c){
    mContext = c;
  }

}

【讨论】:

    【解决方案2】:

    您可以在适配器构造函数中传递对 Context 的引用并将其存储,也可以在方法 getView() 中直接从父级引用它

    public class ToDoListAdapter extends BaseAdapter {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         //inflate layout
        }
    }
    

    【讨论】:

      【解决方案3】:

      你可以尝试实例化一个变量来带来这样的上下文内容:

      在 getView() 视图视图 = LayoutInflater.from(parent.getContext()).inflate(R.layout.itemtutor, null);

      并创建一个上下文变量: 上下文 mycontext = view.getContext();

      【讨论】:

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