getView中int位置的重要性

时间:2013-04-17 14:45:19

标签: java android

数组适配器的getView函数中position属性的意义是什么?我坚持一个例子,其中getView一次又一次地被调用,值为0,而传递给它的列表有多个元素。 请帮忙

private ArrayAdapter<Connection> buildConnectionAdapter(final ArrayList<Connection> connectionList) {
        ArrayAdapter<Connection> attachmentadapter = new ArrayAdapter<Connection>(this, R.layout.text_view, R.id.dummy, connectionList) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
                final String value;
                Connection obj = connectionList.get(position);
                value = obj.getValue();
                TextView tv = new TextView(UserGlobalAppSettings.this);
                SpannableString str = makeLinkSpan(value, new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                    }
                });
                str.setSpan(new URLSpan(""), 0, value.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
                tv.setText(str);
                tv.setTag(obj);
                return tv;
            }
        };
        return attachmentadapter;

    }

布局就像这样

  <ListView
                        android:id="@+id/im_services_list"
                        android:layout_width="wrap_content"
                        android:layout_height="fill_parent" >
                    </ListView>

2 个答案:

答案 0 :(得分:0)

position属性定义了在list / spinner中单击的项目的位置,如果你的列表显示0项,即使正确传递了多个项目的列表,我建议检查适配器的getcount方法,是否返回计数是否通过的名单。

答案 1 :(得分:0)

得到了答案......根据谷歌的哲学,我们不打算在彼此之间使用两个可滚动的项目!!!! ...但是alrteady写了很多代码和时间,所以这样做了!! ...

public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        // pre-condition
        return;
    }
    int totalHeight = 0;
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
}