id的getSystemService自定义ArrayAdapt

id的getSystemService自定义ArrayAdapt

本文介绍了Android的getSystemService自定义ArrayAdapter内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个自定义ArrayAdapter参考这里我的code是

 包com.example.AndTest;

进口的java.util.ArrayList;

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

公共类CategoryAdapter扩展ArrayAdapter<类别> {
    私人的ArrayList<类别>项目;

    公共CategoryAdapter(上下文的背景下,INT textViewResourceId,
            ArrayList的<类别>项目){
        超(背景下,textViewResourceId,项目);
        this.items =项目;
    }

    @覆盖
    公共查看getView(INT位置,查看convertView,ViewGroup中父){
        视图V = convertView;
        如果(V == NULL){
            LayoutInflater VI =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            V = vi.inflate(R.layout.list,NULL);
        }
        C类= items.get(位置);
        如果(C!= NULL){
            TextView中的itemId =(TextView中)v.findViewById(R.id.itemid);
            TextView的itemLabel =(TextView中)v.findViewById(R.id.itemlabel);
            如果(的itemId!= NULL){
                itemId.setText(姓名:+ c.getId());
            }
            如果(itemLabel!= NULL){
                itemLabel.setText(状态:+ c.getTitle());
            }
        }
        返回伏;
    }
}
 

不过,我收到错误消息,上线 LayoutInflater VI =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

的方法getSystemService(字符串)是未定义CategoryAdapter类型

我缺少的东西。

 公共类分类实现Parcelable {
....
}
 

解决方案

getSystemService 上下文的方法类,所以尽量获得上下文对象第一:

  LayoutInflater VI =(LayoutInflater)的getContext()getSystemService(Context.LAYOUT_INFLATER_SERVICE)。
 

I was trying to write a custom ArrayAdapter referencing hereMy code is

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;
    }
}

But I'm getting error message on the lineLayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

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

Am i missing something..

public class Category implements Parcelable {
....
}
解决方案

getSystemService is a method of Context class, so try to get a Context object first:

LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

这篇关于Android的getSystemService自定义ArrayAdapter内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 00:37