Android - CustomAdapter<MyClass> 不获取对象

标签 android json arraylist android-arrayadapter adapter

我创建了一个自定义适配器以将我的元素发送到我的适配器类。

我的项目:

  public ItemAcompanhe(String dataPar, String cabecalhoPar, String descricaoPar, int imagePar){
    this.data = dataPar;
    this.cabecalho = cabecalhoPar;
    this.descricao = descricaoPar;
    this.image = imagePar;
}

我的适配器

 public AdapterAcompanheSantana(Context context, int resource, ArrayList<ItemAcompanhe> objects/*, String[] objects2*/) {
    //super(context, resource, objects);
    super(context, resource, objects);
}

我设置 itens 的适配器:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v=((Activity)getContext()).getLayoutInflater().inflate(R.layout.item_list_acompanhe,null);

    ItemAcompanhe holder = null;
    holder = new ItemAcompanhe(jsonItemAcompanhe);

    // ImageView     item_acompanhe_btn_topo
    ImageView img = (ImageView) v.findViewById(R.id.item_acompanhe_btn_topo);
    img.setBackgroundResource(image[position]);

    // TextView      item_acompanhe_txt_cabecalho   CABECALHO DESCRICAO
    TextView textViewTituloAgendaFragItem = (TextView) v.findViewById(R.id.item_acompanhe_txt_cabecalho);
    textViewTituloAgendaFragItem.setText(listaDeCabecalhos[position]);

    // TextView      item_acompanhe_txt_data
    TextView textViewDatasAgendaFragItem = (TextView) v.findViewById(R.id.item_acompanhe_txt_data);
    textViewDatasAgendaFragItem.setText(listaDeDatas[position]);

    // TextView      item_acompanhe_txt_conteudo
    TextView textViewDescricaoAgendaFragItem = (TextView) v.findViewById(R.id.item_acompanhe_txt_conteudo);
    textViewDescricaoAgendaFragItem.setText(listaDeDescricao[position]);

    Typeface font = Typeface.createFromAsset(getContext().getAssets(), "fonts/OpenSans-Regular.ttf");
    textViewDescricaoAgendaFragItem.setTypeface(font);



    return v;
}

我无法从我的列表中获取任何项目,并将其设置为适配器中的参数。 任何人都知道我是如何解决这个问题的?

最佳答案

这个

ItemAcompanhe holder = null;
holder = new ItemAcompanhe(jsonItemAcompanhe);

应替换为

ItemAcompanhe holder = getItem(position);

这将从数据集中获取一个项目(在您的情况下由父类(super class)处理)。我建议您避免在每次调用 getView 时都膨胀一个新的 convertView,但只有当 convertView 为 null 时

 if (convertView == null) {     
    convertView = ((Activity)getContext()).getLayoutInflater().inflate(R.layout.item_list_acompanhe,null);
 }

并返回convertView

关于Android - CustomAdapter<MyClass> 不获取对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33245086/

相关文章:

json - 通过 bash 脚本更改 json 文件

java - Java中的JSON解析找不到项目

java - 使用冒泡排序与 fork/join 池并行对 Arraylist 进行排序

java - Android 将 Arraylist 数据设置为自定义适配器

java - 如何使用 Java 将子元素附加到大型 XML 文件?

android - 编译Android Qemu镜像

android - React-Native - 特定页面上的后退按钮处理程序

javascript - 无法解决错误 "Uncaught SyntaxError: Unexpected token o "

java - 像 xml schema java 一样对列表进行排序

android - 基于 REST 的客户端-服务器同步