如何在android

时间:2017-01-09 13:30:38

标签: android android-layout listview

点击Button ADD ANOTHER,如屏幕截图所示 它将相继添加另一种药物。问题是,我无法显示多个数据。

如何使用列表视图显示任何帮助。

提前谢谢

enter image description here

1 个答案:

答案 0 :(得分:1)

简单地说,在ArrayList中添加您在适配器构造函数中传递的数据。之后调用adapter.notifyDataSetChanged()。如果您遇到列表视图高度问题,请在按钮单击时动态设置 -

public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
    return;

int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED);
int totalHeight = 0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++) {
    view = listAdapter.getView(i, view, listView);
    if (i == 0)
        view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, LayoutParams.WRAP_CONTENT));

    view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
    totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}