Android - CustomAdapter <myclass>没有得到对象

时间:2015-10-20 19:18:41

标签: android json arraylist android-arrayadapter adapter

我创建了一个自定义适配器来将我的itens发送到我的适配器类。

我的项目:

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

MyAdapter

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

我无法从我的列表中获取任何项目作为参数在适配器中。 有谁知道我如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

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

应替换为

ItemAcompanhe holder = getItem(position);

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

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

并返回convertView

答案 1 :(得分:0)

我用以下方法解决了这个问题:

 Myclass extends ArrayAdapter<Item> 

适配器创建者

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

GetView:

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

    Item atual = getItem(position);