0

数组适配器的getView函数中位置属性的意义是什么?我遇到了一个例子,其中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>
4

2 回答 2

0

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

于 2013-04-17T15:02:41.877 回答
0

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

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);
}
于 2013-04-19T11:58:43.807 回答