2016-10-29 3 views
0

Im совершенно новое в андроид студии, и Im пытается создать пользовательский элемент управления ListView, проблема заключается в том, что он создает неправильный список, так какпользовательских 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 код:

общественного класса custom_row расширяет 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; 
} 

}

+0

Что 'класс custom_row'? – Vyacheslav

+0

Здесь не хватает деталей, чтобы помочь вам. Нам нужно увидеть класс 'custom_row' (который не является строкой, это Adapter и отображает много строк, поэтому я бы предложил переименовать это). Тогда нам также нужно увидеть XML-макет одной строки –

+0

Пожалуйста, покажите [mcve] с [править] на ваш вопрос –

ответ

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

Большое спасибо! Помогает! – Rufat

+0

Проблема заключается в том, что я добавляю searchBox, который ищет только столбцы имен, возможно ли также выполнить поиск по электронной почте? – Rufat