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

我的项目:

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


我在iten上设置的适配器:

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


我无法从设置为Adapter中参数的列表中获得任何项目。
有人知道我该如何解决吗?

最佳答案

这个

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


应该替换为

ItemAcompanhe holder = getItem(position);


它将从数据集中获取一个项目(在您的情况下,由超类处理)。我建议您避免每次调用getView时都要为新的convertView充气,但前提是convertView为null

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


并返回convertView

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

10-10 17:15