2013-06-07 3 views
0

TextView.setText устанавливает все элементы в списке как одно и то же. Как создать список всех элементов из моего ArrayList? Мне нужно как-то указать позицию?CustomAdapter setText неверные значения

public class CustomAdapter extends BaseAdapter { 

    private Context ctx; 
    private ArrayList<String> children; 

    CustomAdapter (ArrayList<String> data, Context context, String log) { 
     this.ctx = context; 
     this.children = data; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent){ 

      LayoutInflater inflator = (LayoutInflater)ctx.getSystemService(LAYOUT_INFLATER_SERVICE); 
      View v = inflator.inflate(R.layout.text_list, null); 

      TextView textView = (TextView) v.findViewById(R.id.logText); 
      System.out.println("Cyan"); 
      textView.setTextColor(Color.CYAN); 
      System.out.println("LOG SIZE: " + log.size()); 

      for(int i = 0; i < children.size(); i++){ 
       textView.setText(children.get(i)); 
      } 
      return textView; 
    } 

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

    @Override 
    public Object getItem(int position) { 

     // TODO Auto-generated method stub 
     return position; 
    } 

    @Override 
    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 
} 

}

+0

Небольшой оффтоп, но не делают этого: View v = inflator.inflate (R.layout.text_list, нуль); Использование Просмотр v = инфлятор.inflate (R.layout.text_list, parent); http://www.doubleencore.com/2013/05/layout-inflation-as-intended/?utm_source=%23AndroidDev+Weekly&utm_campaign=609fad7368-NEWSLETTER&utm_medium=email&utm_term=0_f921dd69d1-609fad7368-61796877 – ADK

ответ

3

getView() вызывается для каждой строки, проходящей в положении пункта следует использовать. Вы повторяете все ваши дети для каждой строки, что означает, что он несколько раз устанавливает текст TextView и приземляется на текст последнего ребенка. Ваш метод не должен выглядеть следующим образом:

@Override 
public View getView(int position, View convertView, ViewGroup parent){ 

    if(convertView == null) 
    { 
     LayoutInflater inflator = (LayoutInflater)ctx.getSystemService(LAYOUT_INFLATER_SERVICE); 
     convertView = inflator.inflate(R.layout.text_list, null); 
    } 

    TextView textView = (TextView)convertView.findViewById(R.id.logText); 
    System.out.println("Cyan"); 
    textView.setTextColor(Color.CYAN); 
    System.out.println("LOG SIZE: " + log.size()); 
    textView.setText(children.get(position)); 

    return convertView; 
} 
Смежные вопросы