如何使列表更好?

时间:2011-07-18 06:08:17

标签: android listview

我有这个代码,但我无法找到它是如何工作特别获取视图方法。 我也无法找出什么是public View getView(int position,View convertView,         ViewGroup父级)

public class DynamicDemo extends ListActivity {
    TextView selection;
    String[] items={"lorem", "ipsum", "dolor", "sit", "amet",
    "consectetuer", "adipiscing", "elit", "morbi", "vel",
    "ligula", "vitae", "arcu", "aliquet", "mollis",
    "etiam", "vel", "erat", "placerat", "ante",
    "porttitor", "sodales", "pellentesque", "augue",
    "purus"};

    @Override
    public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    setListAdapter(new IconicAdapter(this));
    selection=(TextView)findViewById(R.id.selection);
    }
    public void onListItemClick(ListView parent, View v,
    int position, long id) {
    selection.setText(items[position]);
    }
    class IconicAdapter extends ArrayAdapter {
    Activity context;
    IconicAdapter(Activity context) {
    super(context, R.layout.row, items);
    this.context=context;
    }
    public View getView(int position, View convertView,
    ViewGroup parent) {
    LayoutInflater inflater=context.getLayoutInflater();
    View row=inflater.inflate(R.layout.row, null);
    TextView label=(TextView)row.findViewById(R.id.label);
    label.setText(items[position]);
    if (items[position].length()>4) {
    ImageView icon=(ImageView)row.findViewById(R.id.icon);
    icon.setImageResource(R.drawable.delete);
    }
    return(row);
    }
    }
    }

1 个答案:

答案 0 :(得分:1)

当ListView的每一行获得可见性时,会为其调用getView()方法通常会像在代码中那样对xml进行充气

  

参数说明:

int position

适配器中的位置

View convertView

用于回收行项目

ViewGroup parent

将视图添加到的父视图组。