gpt4 book ai didi

java - 如何修复 Layoutinflator 未找到错误?

转载 作者:行者123 更新时间:2023-12-02 01:33:02 25 4
gpt4 key购买 nike

我正在尝试显示自定义列表。我正在将字符串数组传递给类 customAdapter。当我尝试访问 getLayoutInflater 时,它没有被识别?我该如何解决这个问题

package com.parse.starter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;

public class CustomAdapter extends BaseAdapter {
ArrayList<String> descriptions;
public CustomAdapter(final ArrayList<String> description) {
descriptions=description;
}

@Override
public int getCount() {

return descriptions.size();
}

@Override
public Object getItem(int position) {
return null;
}

@Override
public long getItemId(int position) {
return 0;
}

@Override
public View getView(int position, View view, ViewGroup parent) {
view=getLayotInflater(R.layout.rowcourses,null);
return null;
}
}

最佳答案

在这里,更改这个构造函数

private Context context;
public CustomAdapter(Context context, final ArrayList<String> description) {
descriptions=description;
context = context;
}

并修改它

@Override
public View getView(int position, View view, ViewGroup parent) {
// so you would require an Inflater service which will load/inflate the layout, and to use service,
// you need to have access to context, which you can get it from the constructor by passing it from your host class.

LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View aView=inflater.inflate(R.layout.rowcourses, parent,false);
// once you load the view, you need to return that, in your case you were returning null
return aView;
}

关于java - 如何修复 Layoutinflator 未找到错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55748315/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com