自定义列表视图,查看问题

时间:2016-10-29 11:05:25

标签: java android listview

我是android studio中的新手,我试图创建自定义listview,问题是它创建了错误的列表,如

Name
Name
------
Surname
Surname
-----

...并重复

我想做的是:

Name
Surname
-------

我的代码是:

    protected void onResume() {
    DBHelper db = new DBHelper(this);
    ArrayList<String> names = new ArrayList<String>();

    for (int i = 0; i < db.getAllContacts().size(); i++) {
        names.add(db.getAllContacts().get(i).getName());
                names.add(db.getAllContacts().get(i).getEmail());

    }
        ArrayAdapter adapter = new custom_row(this, names);
        listView_allContacts.setAdapter(adapter);

        super.onResume();
    }

这里有什么问题?提前谢谢!

我的custom_row代码:

public class custom_row extends ArrayAdapter {

public custom_row(Context context, ArrayList<String> names) {
    super(context, R.layout.custom_cell, names);

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater peoInf = LayoutInflater.from(getContext());
    View customView = peoInf.inflate(R.layout.custom_cell, parent , false);
    String singlePeople = getItem(position);
    TextView name_text = (TextView) customView.findViewById(R.id.name_text);
    TextView email_text = (TextView) customView.findViewById(R.id.email_text);

    name_text.setText(singlePeople);
    email_text.setText(singlePeople);
    return customView;
}

}

1 个答案:

答案 0 :(得分:0)

您需要两个不同的ArrayList,一个用于名称,一个用于电子邮件。

试试这个

ArrayList<String> list;

public custom_row(Context context, ArrayList<String> names, ArrayList<String> email) {
    super(context, R.layout.custom_cell, names);
    list = email;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater peoInf = LayoutInflater.from(getContext());
    View customView = peoInf.inflate(R.layout.custom_cell, parent , false);
    String singlePeople = getItem(position);
    String email = list.get(position);
    TextView name_text = (TextView) customView.findViewById(R.id.name_text);
    TextView email_text = (TextView) customView.findViewById(R.id.email_text);

     name_text.setText(singlePeople);
    email_text.setText(email);
    return customView;
}