0

textView.setText 将列表中的所有项目设置为同一事物。如何从我的 ArrayList 创建一个包含所有元素的列表?我需要以某种方式指定位置吗?

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

}

4

1 回答 1

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;
}
于 2013-06-07T15:14:42.513 回答