首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >安卓getSystemService自定义ArrayAdapter内幕

安卓getSystemService自定义ArrayAdapter内幕
EN

Stack Overflow用户
提问于 2010-12-01 13:00:43
回答 2查看 24.8K关注 0票数 16

我正在尝试编写一个引用here的自定义ArrayAdapter,我的代码是

代码语言:javascript
复制
package com.example.AndTest;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class CategoryAdapter extends ArrayAdapter<Category> {
    private ArrayList<Category> items;

    public CategoryAdapter(Context context, int textViewResourceId,
            ArrayList<Category> items) {
        super(context, textViewResourceId, items);
        this.items = items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.list, null);
        }
        Category c = items.get(position);
        if (c != null) {
            TextView itemId = (TextView) v.findViewById(R.id.itemid);
            TextView itemLabel = (TextView) v.findViewById(R.id.itemlabel);
            if (itemId != null) {
                itemId.setText("Name: " + c.getId());
            }
            if (itemLabel != null) {
                itemLabel.setText("Status: " + c.getTitle());
            }
        }
        return v;
    }
}

但是我在LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);线路上收到了错误信息

The method getSystemService(String) is undefined for the type CategoryAdapter

我是不是错过了什么..。

代码语言:javascript
复制
public class Category implements Parcelable {
....
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-12-01 13:26:40

getSystemServiceContext类的一个方法,所以先尝试获取一个Context对象:

代码语言:javascript
复制
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
票数 79
EN

Stack Overflow用户

发布于 2015-01-05 22:10:30

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4321343

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档