在Android中使用适配器将两个列表视图合并为一个

时间:2014-08-16 18:57:18

标签: java android arraylist android-arrayadapter baseadapter

我正在尝试使用适配器将2个列表视图合并为一个,“ContatosTelefoneModel”工作正常,它显示列表中的所有电话,问题是电子邮件,它只显示最后一个值数组,我会添加一些图片来更好地解释它:

enter image description here我有一个包含4封电子邮件的数组,steve1,steve2,steve3和steve4,我只是得到steve4,我非常非常非常喜欢Android开发,我想知道如何正确显示电子邮件数组。

public class TelefoneListAdapter extends BaseAdapter  {

    private ArrayList<ContatosTelefoneModel> listD;

    private ArrayList<ContatosEmailModel> email;

    private LayoutInflater layoutInflater;

    private Context mContext;

    public TelefoneListAdapter(Context context, ArrayList<ContatosTelefoneModel> listData, ArrayList<ContatosEmailModel> emails) {
        this.listD = listData;
        this.email = emails;
        layoutInflater = LayoutInflater.from(context);
        mContext = context;
    }

    @Override
    public int getCount() {
        return listD.size();

    }

    @Override
    public Object getItem(int position) {
        return listD.get(position);
    }

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

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.tel_row_layout, null);
            holder = new ViewHolder();
            holder.headlineView = (TextView) convertView.findViewById(R.id.numero);
            holder.reportedDateView = (TextView) convertView.findViewById(R.id.tipo);
            holder.emailView = (TextView) convertView.findViewById(R.id.email);
            holder.IdTelefone = (TextView) convertView.findViewById(R.id.TelefoneID);


            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

            ContatosTelefoneModel newsItem = listD.get(position);
            holder.headlineView.setText(newsItem.getNumero());
            holder.reportedDateView.setText(newsItem.getTipo());




        for(int x=0; x<email.size(); x++) {

           ContatosEmailModel mContatosEmailModel = email.get(x);
            holder.emailView.setText(mContatosEmailModel.getEmail());




        }

       return convertView;
    }

    static class ViewHolder {
        TextView headlineView;
        TextView reportedDateView;
        TextView emailView;
        TextView IdTelefone;
        TextView ContatoCliente;

    }
}

型号:

public class ContatosEmailModel implements Serializable {

    private int id;
    private int id_rm;
    private String email;
    private String tipo;


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getId_rm() {
        return id_rm;
    }

    public void setId_rm(int id_rm) {
        this.id_rm = id_rm;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getTipo() {
        return tipo;
    }

    public void setTipo(String tipo) {
        this.tipo = tipo;
    }

    @Override
    public String toString() {
        return "ContatosEmailModel{" +
                "id=" + id +
                ", id_rm=" + id_rm +
                ", email='" + email + '\'' +
                ", tipo='" + tipo + '\'' +
                '}';
    }
}

public class ContatosTelefoneModel implements Serializable{

    private int id;
    private int id_rm;
    private String numero;
    private String tipo;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getId_rm() {
        return id_rm;
    }

    public void setId_rm(int id_rm) {
        this.id_rm = id_rm;
    }

    public String getNumero() {
        return numero;
    }

    public void setNumero(String numero) {
        this.numero = numero;
    }

    public String getTipo() {
        return tipo;
    }

    public void setTipo(String tipo) {
        this.tipo = tipo;
    }

    @Override
    public String toString() {
        return "ContatosTelefoneModel{" +
                "id=" + id +
                ", id_rm=" + id_rm +
                ", numero='" + numero + '\'' +
                ", tipo='" + tipo + '\'' +
                '}';
    }
}

2 个答案:

答案 0 :(得分:1)

你为什么在这里使用for循环?

    for(int x=0; x<email.size(); x++) {
       ContatosEmailModel mContatosEmailModel = email.get(x);
        holder.emailView.setText(mContatosEmailModel.getEmail());
    }

与标题视图和reportsDateView

一样

更改此代码:

  

holder.emailView.setText(this.email.get(位置).getEmail());

    for(int x=0; x<email.size(); x++) {

       ContatosEmailModel mContatosEmailModel = email.get(x);
        holder.emailView.setText(mContatosEmailModel.getEmail());
    }

到此:

holder.emailView.setText(mContatosEmailModel.getEmail());

答案 1 :(得分:0)

如果两个数组的大小相同,则删除&#34; for&#34;循环在这里:

  for(int x=0; x<email.size(); x++) {
      ContatosEmailModel mContatosEmailModel = email.get(x);
        holder.emailView.setText(mContatosEmailModel.getEmail());
  }

这是一个问题,因为每次显示一个项目时,您只会获得最后一个项目。你可以改变它:

  //for(int x=0; x<email.size(); x++) {
      ContatosEmailModel mContatosEmailModel = email.get(position);
        holder.emailView.setText(mContatosEmailModel.getEmail());
  //}

我建议将它们放入一个将两个值保持在同一位置的abject数组中。

或者您可以进行某种匹配 - 但是您如何知道哪个电子邮件的名称正确?在这种情况下,我已经回答了您的问题,为什么会发生这种情况,但您无法解释您从何处获取数据或如何将电子邮件与每个电话号码相匹配。